blob: f30145eece6bce5f56c41bb76785507a51e509ff [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"
John Stiles44e96be2020-08-31 13:16:04 -040011#include <iterator>
John Stilesfbd050b2020-08-03 13:21:46 -040012#include <memory>
Ethan Nicholasaf197692017-02-27 13:26:45 -050013#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070014
John Stiles6bef6a72020-12-02 14:26:04 -050015#include "include/private/SkTArray.h"
Ethan Nicholas6e0fa402020-08-20 14:08:23 -040016#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/sksl/SkSLCompiler.h"
18#include "src/sksl/SkSLParser.h"
Brian Osman3000d6b2020-07-31 15:57:28 -040019#include "src/sksl/SkSLUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/ir/SkSLBinaryExpression.h"
21#include "src/sksl/ir/SkSLBoolLiteral.h"
22#include "src/sksl/ir/SkSLBreakStatement.h"
23#include "src/sksl/ir/SkSLConstructor.h"
24#include "src/sksl/ir/SkSLContinueStatement.h"
25#include "src/sksl/ir/SkSLDiscardStatement.h"
26#include "src/sksl/ir/SkSLDoStatement.h"
27#include "src/sksl/ir/SkSLEnum.h"
28#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040029#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040030#include "src/sksl/ir/SkSLExternalValueReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/sksl/ir/SkSLField.h"
32#include "src/sksl/ir/SkSLFieldAccess.h"
33#include "src/sksl/ir/SkSLFloatLiteral.h"
34#include "src/sksl/ir/SkSLForStatement.h"
35#include "src/sksl/ir/SkSLFunctionCall.h"
36#include "src/sksl/ir/SkSLFunctionDeclaration.h"
37#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050038#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/sksl/ir/SkSLFunctionReference.h"
40#include "src/sksl/ir/SkSLIfStatement.h"
41#include "src/sksl/ir/SkSLIndexExpression.h"
42#include "src/sksl/ir/SkSLIntLiteral.h"
43#include "src/sksl/ir/SkSLInterfaceBlock.h"
44#include "src/sksl/ir/SkSLLayout.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060045#include "src/sksl/ir/SkSLNop.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/ir/SkSLNullLiteral.h"
47#include "src/sksl/ir/SkSLPostfixExpression.h"
48#include "src/sksl/ir/SkSLPrefixExpression.h"
49#include "src/sksl/ir/SkSLReturnStatement.h"
50#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050051#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/sksl/ir/SkSLSwitchCase.h"
53#include "src/sksl/ir/SkSLSwitchStatement.h"
54#include "src/sksl/ir/SkSLSwizzle.h"
55#include "src/sksl/ir/SkSLTernaryExpression.h"
56#include "src/sksl/ir/SkSLUnresolvedFunction.h"
57#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058#include "src/sksl/ir/SkSLVariable.h"
59#include "src/sksl/ir/SkSLVariableReference.h"
60#include "src/sksl/ir/SkSLWhileStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070061
62namespace SkSL {
63
64class AutoSymbolTable {
65public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050066 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070067 : fIR(ir)
68 , fPrevious(fIR->fSymbolTable) {
69 fIR->pushSymbolTable();
70 }
71
72 ~AutoSymbolTable() {
73 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040074 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070075 }
76
77 IRGenerator* fIR;
78 std::shared_ptr<SymbolTable> fPrevious;
79};
80
ethannicholas22f939e2016-10-13 13:25:34 -070081class AutoLoopLevel {
82public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050083 AutoLoopLevel(IRGenerator* ir)
ethannicholas22f939e2016-10-13 13:25:34 -070084 : fIR(ir) {
85 fIR->fLoopLevel++;
86 }
87
88 ~AutoLoopLevel() {
89 fIR->fLoopLevel--;
90 }
91
92 IRGenerator* fIR;
93};
94
Ethan Nicholasaf197692017-02-27 13:26:45 -050095class AutoSwitchLevel {
96public:
97 AutoSwitchLevel(IRGenerator* ir)
98 : fIR(ir) {
99 fIR->fSwitchLevel++;
100 }
101
102 ~AutoSwitchLevel() {
103 fIR->fSwitchLevel--;
104 }
105
106 IRGenerator* fIR;
107};
108
John Stiles194b9b92020-09-15 15:37:24 -0400109static void fill_caps(const SkSL::ShaderCapsClass& caps,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400110 std::unordered_map<String, Program::Settings::Value>* capsMap) {
Brian Osman88cda172020-10-09 12:05:16 -0400111#define CAP(name) capsMap->insert({String(#name), Program::Settings::Value(caps.name())})
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500112 CAP(fbFetchSupport);
Brian Salomond4013302018-04-04 13:58:33 +0000113 CAP(fbFetchNeedsCustomOutput);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500114 CAP(flatInterpolationSupport);
115 CAP(noperspectiveInterpolationSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500116 CAP(externalTextureSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500117 CAP(mustEnableAdvBlendEqs);
118 CAP(mustEnableSpecificAdvBlendEqs);
119 CAP(mustDeclareFragmentShaderOutput);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400120 CAP(mustDoOpBetweenFloorAndAbs);
Brian Salomonf8c187c2019-12-19 14:41:57 -0500121 CAP(mustGuardDivisionEvenAfterExplicitZeroCheck);
122 CAP(inBlendModesFailRandomlyForAllZeroVec);
Michael Ludwig24d438b2018-09-12 15:22:50 -0400123 CAP(atan2ImplementedAsAtanYOverX);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500124 CAP(canUseAnyFunctionInShader);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700125 CAP(floatIs32Bits);
Ethan Nicholas07990de2017-07-18 09:47:43 -0400126 CAP(integerSupport);
John Stiles6f3015a2020-10-08 14:55:36 -0400127 CAP(builtinFMASupport);
128 CAP(builtinDeterminantSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500129#undef CAP
130}
131
Brian Osman0006ad02020-11-18 15:38:39 -0500132IRGenerator::IRGenerator(const Context* context,
133 const ShaderCapsClass* caps,
Brian Osman0006ad02020-11-18 15:38:39 -0500134 ErrorReporter& errorReporter)
135 : fContext(*context)
136 , fCaps(caps)
Brian Osman0006ad02020-11-18 15:38:39 -0500137 , fErrors(errorReporter)
138 , fModifiers(new ModifiersPool()) {
Brian Osman0006ad02020-11-18 15:38:39 -0500139 if (fCaps) {
140 fill_caps(*fCaps, &fCapsMap);
141 } else {
142 fCapsMap.insert({String("integerSupport"), Program::Settings::Value(true)});
143 }
144
145}
146
147void IRGenerator::pushSymbolTable() {
148 auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
149 fSymbolTable = std::move(childSymTable);
150}
151
152void IRGenerator::popSymbolTable() {
153 fSymbolTable = fSymbolTable->fParent;
154}
155
Ethan Nicholasfc994162019-06-06 10:04:27 -0400156std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, StringFragment name) {
Brian Osman16f376f2020-09-02 12:30:59 -0400157 if (fKind != Program::kFragment_Kind &&
158 fKind != Program::kVertex_Kind &&
159 fKind != Program::kGeometry_Kind) {
160 fErrors.error(offset, "extensions are not allowed here");
161 return nullptr;
162 }
163
John Stilesfbd050b2020-08-03 13:21:46 -0400164 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700165}
166
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400167std::unique_ptr<ModifiersPool> IRGenerator::releaseModifiers() {
168 std::unique_ptr<ModifiersPool> result = std::move(fModifiers);
169 fModifiers = std::make_unique<ModifiersPool>();
170 return result;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400171}
172
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400173std::unique_ptr<Statement> IRGenerator::convertSingleStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700174 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400175 case ASTNode::Kind::kBlock:
176 return this->convertBlock(statement);
177 case ASTNode::Kind::kVarDeclarations:
178 return this->convertVarDeclarationStatement(statement);
179 case ASTNode::Kind::kIf:
180 return this->convertIf(statement);
181 case ASTNode::Kind::kFor:
182 return this->convertFor(statement);
183 case ASTNode::Kind::kWhile:
184 return this->convertWhile(statement);
185 case ASTNode::Kind::kDo:
186 return this->convertDo(statement);
187 case ASTNode::Kind::kSwitch:
188 return this->convertSwitch(statement);
189 case ASTNode::Kind::kReturn:
190 return this->convertReturn(statement);
191 case ASTNode::Kind::kBreak:
192 return this->convertBreak(statement);
193 case ASTNode::Kind::kContinue:
194 return this->convertContinue(statement);
195 case ASTNode::Kind::kDiscard:
196 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500197 case ASTNode::Kind::kType:
198 // TODO: add IRNode for struct definition inside a function
199 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400200 default:
201 // it's an expression
202 std::unique_ptr<Statement> result = this->convertExpressionStatement(statement);
Ethan Nicholase6592142020-09-08 10:22:09 -0400203 if (fRTAdjust && fKind == Program::kGeometry_Kind) {
204 SkASSERT(result->kind() == Statement::Kind::kExpression);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400205 Expression& expr = *result->as<ExpressionStatement>().expression();
Ethan Nicholase6592142020-09-08 10:22:09 -0400206 if (expr.kind() == Expression::Kind::kFunctionCall) {
John Stiles403a3632020-08-20 12:11:48 -0400207 FunctionCall& fc = expr.as<FunctionCall>();
Ethan Nicholased84b732020-10-08 11:45:44 -0400208 if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") {
John Stiles8f2a0cf2020-10-13 12:48:21 -0400209 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400210 statements.reserve_back(2);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000211 statements.push_back(getNormalizeSkPositionCode());
212 statements.push_back(std::move(result));
John Stilesfbd050b2020-08-03 13:21:46 -0400213 return std::make_unique<Block>(statement.fOffset, std::move(statements),
214 fSymbolTable);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000215 }
216 }
217 }
218 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700219 }
220}
221
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400222std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
John Stiles8f2a0cf2020-10-13 12:48:21 -0400223 StatementArray oldExtraStatements = std::move(fExtraStatements);
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400224 std::unique_ptr<Statement> result = this->convertSingleStatement(statement);
225 if (!result) {
226 fExtraStatements = std::move(oldExtraStatements);
227 return nullptr;
228 }
229 if (fExtraStatements.size()) {
230 fExtraStatements.push_back(std::move(result));
John Stiles8f2a0cf2020-10-13 12:48:21 -0400231 auto block = std::make_unique<Block>(/*offset=*/-1, std::move(fExtraStatements),
232 /*symbols=*/nullptr, /*isScope=*/false);
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400233 fExtraStatements = std::move(oldExtraStatements);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400234 return std::move(block);
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400235 }
236 fExtraStatements = std::move(oldExtraStatements);
237 return result;
238}
239
Ethan Nicholasfc994162019-06-06 10:04:27 -0400240std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
241 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700242 AutoSymbolTable table(this);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400243 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400244 for (const auto& child : block) {
245 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246 if (!statement) {
247 return nullptr;
248 }
249 statements.push_back(std::move(statement));
250 }
John Stilesfbd050b2020-08-03 13:21:46 -0400251 return std::make_unique<Block>(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700252}
253
Ethan Nicholasfc994162019-06-06 10:04:27 -0400254std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
255 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400256 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400257 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700258 return nullptr;
259 }
Brian Osmanc0213602020-10-06 14:43:32 -0400260 if (decls.size() == 1) {
261 return std::move(decls.front());
262 } else {
263 return std::make_unique<Block>(s.fOffset, std::move(decls), /*symbols=*/nullptr,
264 /*isScope=*/false);
265 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700266}
267
John Stiles8f2a0cf2020-10-13 12:48:21 -0400268StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
269 Variable::Storage storage) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400270 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
John Stilesf621e232020-08-25 13:33:02 -0400271 auto declarationsIter = decls.begin();
272 const Modifiers& modifiers = declarationsIter++->getModifiers();
273 const ASTNode& rawType = *(declarationsIter++);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400274 const Type* baseType = this->convertType(rawType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275 if (!baseType) {
Brian Osmanc0213602020-10-06 14:43:32 -0400276 return {};
ethannicholasb3058bd2016-07-01 08:22:01 -0700277 }
Brian Osman82329002020-07-21 09:39:27 -0400278 if (baseType->nonnullable() == *fContext.fFragmentProcessor_Type &&
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400279 storage != Variable::Storage::kGlobal) {
Brian Osman82329002020-07-21 09:39:27 -0400280 fErrors.error(decls.fOffset,
281 "variables of type '" + baseType->displayName() + "' must be global");
282 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500283 if (fKind != Program::kFragmentProcessor_Kind) {
John Stiles9aeed132020-11-24 17:36:06 -0500284 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
Brian Osman2fe83fe2019-12-16 13:17:59 -0500285 fErrors.error(decls.fOffset, "'in' variables may not have matrix type");
286 }
Brian Osman088913a2019-12-19 15:44:56 -0500287 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
288 (modifiers.fFlags & Modifiers::kUniform_Flag)) {
289 fErrors.error(decls.fOffset,
290 "'in uniform' variables only permitted within fragment processors");
291 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500292 if (modifiers.fLayout.fWhen.fLength) {
293 fErrors.error(decls.fOffset, "'when' is only permitted within fragment processors");
294 }
295 if (modifiers.fLayout.fFlags & Layout::kTracked_Flag) {
296 fErrors.error(decls.fOffset, "'tracked' is only permitted within fragment processors");
297 }
298 if (modifiers.fLayout.fCType != Layout::CType::kDefault) {
299 fErrors.error(decls.fOffset, "'ctype' is only permitted within fragment processors");
300 }
301 if (modifiers.fLayout.fKey) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400302 fErrors.error(decls.fOffset, "'key' is only permitted within fragment processors");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400303 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500304 }
Brian Osmana4b91692020-08-10 14:26:16 -0400305 if (fKind == Program::kPipelineStage_Kind) {
306 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
307 baseType->nonnullable() != *fContext.fFragmentProcessor_Type) {
308 fErrors.error(decls.fOffset, "'in' variables not permitted in runtime effects");
309 }
310 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500311 if (modifiers.fLayout.fKey && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
312 fErrors.error(decls.fOffset, "'key' is not permitted on 'uniform' variables");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400313 }
Brian Osmanf59a9612020-04-15 14:18:13 -0400314 if (modifiers.fLayout.fMarker.fLength) {
315 if (fKind != Program::kPipelineStage_Kind) {
316 fErrors.error(decls.fOffset, "'marker' is only permitted in runtime effects");
317 }
318 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
319 fErrors.error(decls.fOffset, "'marker' is only permitted on 'uniform' variables");
320 }
321 if (*baseType != *fContext.fFloat4x4_Type) {
322 fErrors.error(decls.fOffset, "'marker' is only permitted on float4x4 variables");
323 }
324 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400325 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
326 if (fKind != Program::kPipelineStage_Kind) {
327 fErrors.error(decls.fOffset, "'srgb_unpremul' is only permitted in runtime effects");
328 }
329 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
330 fErrors.error(decls.fOffset,
331 "'srgb_unpremul' is only permitted on 'uniform' variables");
332 }
333 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500334 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400335 (t.columns() == 3 || t.columns() == 4);
336 };
Ethan Nicholase6592142020-09-08 10:22:09 -0400337 if (!validColorXformType(*baseType) && !(baseType->typeKind() == Type::TypeKind::kArray &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400338 validColorXformType(baseType->componentType()))) {
339 fErrors.error(decls.fOffset,
340 "'srgb_unpremul' is only permitted on half3, half4, float3, or float4 "
341 "variables");
342 }
343 }
Brian Osman3c358422020-03-23 10:44:12 -0400344 if (modifiers.fFlags & Modifiers::kVarying_Flag) {
345 if (fKind != Program::kPipelineStage_Kind) {
346 fErrors.error(decls.fOffset, "'varying' is only permitted in runtime effects");
347 }
348 if (!baseType->isFloat() &&
John Stiles9aeed132020-11-24 17:36:06 -0500349 !(baseType->isVector() && baseType->componentType().isFloat())) {
Brian Osman3c358422020-03-23 10:44:12 -0400350 fErrors.error(decls.fOffset, "'varying' must be float scalar or vector");
351 }
352 }
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400353 int permitted = Modifiers::kConst_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400354 if (storage == Variable::Storage::kGlobal) {
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400355 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
356 Modifiers::kFlat_Flag | Modifiers::kVarying_Flag |
357 Modifiers::kNoPerspective_Flag | Modifiers::kPLS_Flag |
358 Modifiers::kPLSIn_Flag | Modifiers::kPLSOut_Flag |
359 Modifiers::kRestrict_Flag | Modifiers::kVolatile_Flag |
360 Modifiers::kReadOnly_Flag | Modifiers::kWriteOnly_Flag |
361 Modifiers::kCoherent_Flag | Modifiers::kBuffer_Flag;
362 }
363 this->checkModifiers(decls.fOffset, modifiers, permitted);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400364
365 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400366 for (; declarationsIter != decls.end(); ++declarationsIter) {
367 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400368 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
369 (modifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind &&
370 varDecl.getVarData().fName != "sk_FragColor") {
371 fErrors.error(varDecl.fOffset,
Ethan Nicholas6c942712018-03-16 09:45:11 -0400372 "out location=0, index=0 is reserved for sk_FragColor");
373 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400374 const ASTNode::VarData& varData = varDecl.getVarData();
ethannicholasd598f792016-07-25 10:08:54 -0700375 const Type* type = baseType;
John Stiles62a56462020-12-03 10:41:58 -0500376 int arraySize = 0;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400377 auto iter = varDecl.begin();
John Stiles7bd70332020-11-30 17:04:09 -0500378 if (iter != varDecl.end()) {
John Stilesd39aec92020-12-03 14:37:16 -0500379 if (varData.fIsArray) {
John Stiles62a56462020-12-03 10:41:58 -0500380 if (type->isOpaque()) {
381 fErrors.error(type->fOffset,
382 "opaque type '" + type->name() + "' may not be used in an array");
383 }
384 const ASTNode& rawSize = *iter++;
John Stiles7bd70332020-11-30 17:04:09 -0500385 if (rawSize) {
386 auto size = this->coerce(this->convertExpression(rawSize), *fContext.fInt_Type);
387 if (!size) {
388 return {};
389 }
390 String name(type->name());
391 int64_t count;
392 if (!size->is<IntLiteral>()) {
393 fErrors.error(size->fOffset, "array size must be an integer");
394 return {};
395 }
396 count = size->as<IntLiteral>().value();
397 if (count <= 0) {
398 fErrors.error(size->fOffset, "array size must be positive");
399 return {};
400 }
John Stiles62a56462020-12-03 10:41:58 -0500401 arraySize = count;
John Stiles7bd70332020-11-30 17:04:09 -0500402 } else {
John Stiles62a56462020-12-03 10:41:58 -0500403 arraySize = Type::kUnsizedArray;
ethannicholasb3058bd2016-07-01 08:22:01 -0700404 }
John Stiles62a56462020-12-03 10:41:58 -0500405 type = fSymbolTable->addArrayDimensions(type, {arraySize});
ethannicholasb3058bd2016-07-01 08:22:01 -0700406 }
407 }
John Stiles586df952020-11-12 18:27:13 -0500408 auto var = std::make_unique<Variable>(varDecl.fOffset, fModifiers->addToPool(modifiers),
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400409 varData.fName, type, fIsBuiltinCode, storage);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400410 if (var->name() == Compiler::RTADJUST_NAME) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400411 SkASSERT(!fRTAdjust);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400412 SkASSERT(var->type() == *fContext.fFloat4_Type);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000413 fRTAdjust = var.get();
414 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700415 std::unique_ptr<Expression> value;
John Stilese1bbd5c2020-11-17 11:33:49 -0500416 if (iter == varDecl.end()) {
John Stiles62a56462020-12-03 10:41:58 -0500417 if (arraySize == Type::kUnsizedArray) {
John Stilese1bbd5c2020-11-17 11:33:49 -0500418 fErrors.error(varDecl.fOffset,
419 "arrays without an explicit size must use an initializer expression");
420 return {};
421 }
422 } else {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400423 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700424 if (!value) {
Brian Osmanc0213602020-10-06 14:43:32 -0400425 return {};
ethannicholasb3058bd2016-07-01 08:22:01 -0700426 }
John Stiles84d503b2020-11-09 11:19:02 -0500427 if (modifiers.fFlags & Modifiers::kIn_Flag) {
428 fErrors.error(value->fOffset, "'in' variables cannot use initializer expressions");
429 }
ethannicholasd598f792016-07-25 10:08:54 -0700430 value = this->coerce(std::move(value), *type);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500431 if (!value) {
Brian Osmanc0213602020-10-06 14:43:32 -0400432 return {};
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500433 }
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400434 var->setInitialValue(value.get());
ethannicholasb3058bd2016-07-01 08:22:01 -0700435 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400436 const Symbol* symbol = (*fSymbolTable)[var->name()];
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400437 if (symbol && storage == Variable::Storage::kGlobal && var->name() == "sk_FragColor") {
John Stilesce591b72020-08-27 11:47:30 -0400438 // Already defined, ignore.
ethannicholasf789b382016-08-03 12:43:36 -0700439 } else {
John Stiles62a56462020-12-03 10:41:58 -0500440 varDecls.push_back(std::make_unique<VarDeclaration>(var.get(), baseType, arraySize,
441 std::move(value)));
John Stilesb8cc6652020-10-08 09:12:07 -0400442 fSymbolTable->add(std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700443 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700444 }
Brian Osmanc0213602020-10-06 14:43:32 -0400445 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700446}
447
Ethan Nicholasfc994162019-06-06 10:04:27 -0400448std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
Brian Osman16f376f2020-09-02 12:30:59 -0400449 if (fKind != Program::kFragment_Kind &&
450 fKind != Program::kVertex_Kind &&
451 fKind != Program::kGeometry_Kind) {
452 fErrors.error(m.fOffset, "layout qualifiers are not allowed here");
453 return nullptr;
454 }
455
Ethan Nicholasfc994162019-06-06 10:04:27 -0400456 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
457 Modifiers modifiers = m.getModifiers();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400458 if (modifiers.fLayout.fInvocations != -1) {
Ethan Nicholasf06576b2019-04-03 15:45:25 -0400459 if (fKind != Program::kGeometry_Kind) {
460 fErrors.error(m.fOffset, "'invocations' is only legal in geometry shaders");
461 return nullptr;
462 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400463 fInvocations = modifiers.fLayout.fInvocations;
Brian Osmand7e76592020-11-02 12:26:22 -0500464 if (fCaps && !fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400465 modifiers.fLayout.fInvocations = -1;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400466 if (modifiers.fLayout.description() == "") {
467 return nullptr;
468 }
469 }
470 }
Brian Osmand7e76592020-11-02 12:26:22 -0500471 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fCaps &&
472 !fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400473 modifiers.fLayout.fMaxVertices *= fInvocations;
474 }
John Stiles586df952020-11-12 18:27:13 -0500475 return std::make_unique<ModifiersDeclaration>(fModifiers->addToPool(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700476}
477
John Stilesad2319f2020-09-02 15:01:47 -0400478std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
479 SkASSERT(n.fKind == ASTNode::Kind::kIf);
480 auto iter = n.begin();
481 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
482 *fContext.fBool_Type);
483 if (!test) {
484 return nullptr;
485 }
486 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
487 if (!ifTrue) {
488 return nullptr;
489 }
John Stilesad2319f2020-09-02 15:01:47 -0400490 std::unique_ptr<Statement> ifFalse;
491 if (iter != n.end()) {
492 ifFalse = this->convertStatement(*(iter++));
493 if (!ifFalse) {
494 return nullptr;
495 }
John Stilesad2319f2020-09-02 15:01:47 -0400496 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400497 if (test->kind() == Expression::Kind::kBoolLiteral) {
John Stilesad2319f2020-09-02 15:01:47 -0400498 // static boolean value, fold down to a single branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400499 if (test->as<BoolLiteral>().value()) {
John Stilesad2319f2020-09-02 15:01:47 -0400500 return ifTrue;
501 } else if (ifFalse) {
502 return ifFalse;
503 } else {
504 // False & no else clause. Not an error, so don't return null!
John Stiles2ff97062020-09-02 15:02:01 -0400505 return std::make_unique<Nop>();
John Stilesad2319f2020-09-02 15:01:47 -0400506 }
507 }
John Stiles0f464502020-11-20 12:52:22 -0500508 return std::make_unique<IfStatement>(n.fOffset, n.getBool(), std::move(test),
509 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400510}
511
Ethan Nicholasfc994162019-06-06 10:04:27 -0400512std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
513 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholas22f939e2016-10-13 13:25:34 -0700514 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700515 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700516 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400517 auto iter = f.begin();
518 if (*iter) {
519 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700520 if (!initializer) {
521 return nullptr;
522 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700523 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400524 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700525 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400526 if (*iter) {
527 test = this->coerce(this->convertExpression(*iter), *fContext.fBool_Type);
ethannicholas22f939e2016-10-13 13:25:34 -0700528 if (!test) {
529 return nullptr;
530 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700531 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400532 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700533 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400534 if (*iter) {
535 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700536 if (!next) {
537 return nullptr;
538 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700539 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400540 ++iter;
541 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700542 if (!statement) {
543 return nullptr;
544 }
John Stiles0f464502020-11-20 12:52:22 -0500545 return std::make_unique<ForStatement>(f.fOffset, std::move(initializer), std::move(test),
546 std::move(next), std::move(statement), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700547}
548
Ethan Nicholasfc994162019-06-06 10:04:27 -0400549std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
550 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
ethannicholas22f939e2016-10-13 13:25:34 -0700551 AutoLoopLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400552 auto iter = w.begin();
John Stiles0f464502020-11-20 12:52:22 -0500553 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
554 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700555 if (!test) {
556 return nullptr;
557 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400558 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700559 if (!statement) {
560 return nullptr;
561 }
John Stiles0f464502020-11-20 12:52:22 -0500562 return std::make_unique<WhileStatement>(w.fOffset, std::move(test), std::move(statement));
ethannicholasb3058bd2016-07-01 08:22:01 -0700563}
564
Ethan Nicholasfc994162019-06-06 10:04:27 -0400565std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
566 SkASSERT(d.fKind == ASTNode::Kind::kDo);
ethannicholas22f939e2016-10-13 13:25:34 -0700567 AutoLoopLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400568 auto iter = d.begin();
569 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
570 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700571 return nullptr;
572 }
John Stiles0f464502020-11-20 12:52:22 -0500573 std::unique_ptr<Expression> test =
574 this->coerce(this->convertExpression(*(iter++)), *fContext.fBool_Type);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400575 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700576 return nullptr;
577 }
John Stiles0f464502020-11-20 12:52:22 -0500578 return std::make_unique<DoStatement>(d.fOffset, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700579}
580
Ethan Nicholasfc994162019-06-06 10:04:27 -0400581std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
582 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500583 AutoSwitchLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400584 auto iter = s.begin();
585 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500586 if (!value) {
587 return nullptr;
588 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400589 if (value->type() != *fContext.fUInt_Type &&
590 value->type().typeKind() != Type::TypeKind::kEnum) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500591 value = this->coerce(std::move(value), *fContext.fInt_Type);
592 if (!value) {
593 return nullptr;
594 }
595 }
596 AutoSymbolTable table(this);
597 std::unordered_set<int> caseValues;
598 std::vector<std::unique_ptr<SwitchCase>> cases;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400599 for (; iter != s.end(); ++iter) {
600 const ASTNode& c = *iter;
601 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500602 std::unique_ptr<Expression> caseValue;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400603 auto childIter = c.begin();
604 if (*childIter) {
605 caseValue = this->convertExpression(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500606 if (!caseValue) {
607 return nullptr;
608 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400609 caseValue = this->coerce(std::move(caseValue), value->type());
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500610 if (!caseValue) {
611 return nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500612 }
Brian Osman3e3db6c2020-08-14 09:42:12 -0400613 int64_t v = 0;
614 if (!this->getConstantInt(*caseValue, &v)) {
615 fErrors.error(caseValue->fOffset, "case value must be a constant integer");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500616 return nullptr;
617 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500618 if (caseValues.find(v) != caseValues.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700619 fErrors.error(caseValue->fOffset, "duplicate case value");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500620 }
621 caseValues.insert(v);
622 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400623 ++childIter;
John Stiles8f2a0cf2020-10-13 12:48:21 -0400624 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400625 for (; childIter != c.end(); ++childIter) {
626 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500627 if (!converted) {
628 return nullptr;
629 }
630 statements.push_back(std::move(converted));
631 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400632 cases.push_back(std::make_unique<SwitchCase>(c.fOffset, std::move(caseValue),
633 std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500634 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400635 return std::make_unique<SwitchStatement>(s.fOffset, s.getBool(), std::move(value),
636 std::move(cases), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500637}
638
Ethan Nicholasfc994162019-06-06 10:04:27 -0400639std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
640 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700641 if (!e) {
642 return nullptr;
643 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700644 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
645}
646
Ethan Nicholasfc994162019-06-06 10:04:27 -0400647std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
648 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400649 SkASSERT(fCurrentFunction);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000650 // early returns from a vertex main function will bypass the sk_Position normalization, so
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400651 // 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 +0000652 // normalization before each return, but it will probably never actually be necessary.
Ethan Nicholase2c49992020-10-05 11:49:11 -0400653 SkASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->name());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400654 if (r.begin() != r.end()) {
655 std::unique_ptr<Expression> result = this->convertExpression(*r.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -0700656 if (!result) {
657 return nullptr;
658 }
Ethan Nicholased84b732020-10-08 11:45:44 -0400659 if (fCurrentFunction->returnType() == *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700660 fErrors.error(result->fOffset, "may not return a value from a void function");
Brian Osman5eea6ae2020-09-09 16:05:18 -0400661 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700662 } else {
Ethan Nicholased84b732020-10-08 11:45:44 -0400663 result = this->coerce(std::move(result), fCurrentFunction->returnType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700664 if (!result) {
665 return nullptr;
666 }
667 }
668 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
669 } else {
Ethan Nicholased84b732020-10-08 11:45:44 -0400670 if (fCurrentFunction->returnType() != *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700671 fErrors.error(r.fOffset, "expected function to return '" +
Ethan Nicholased84b732020-10-08 11:45:44 -0400672 fCurrentFunction->returnType().displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700673 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700674 return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700675 }
676}
677
Ethan Nicholasfc994162019-06-06 10:04:27 -0400678std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
679 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500680 if (fLoopLevel > 0 || fSwitchLevel > 0) {
John Stilesb61ee902020-09-21 12:26:59 -0400681 return std::make_unique<BreakStatement>(b.fOffset);
ethannicholas22f939e2016-10-13 13:25:34 -0700682 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700683 fErrors.error(b.fOffset, "break statement must be inside a loop or switch");
ethannicholas22f939e2016-10-13 13:25:34 -0700684 return nullptr;
685 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700686}
687
Ethan Nicholasfc994162019-06-06 10:04:27 -0400688std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
689 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
ethannicholas22f939e2016-10-13 13:25:34 -0700690 if (fLoopLevel > 0) {
John Stilesb61ee902020-09-21 12:26:59 -0400691 return std::make_unique<ContinueStatement>(c.fOffset);
ethannicholas22f939e2016-10-13 13:25:34 -0700692 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700693 fErrors.error(c.fOffset, "continue statement must be inside a loop");
ethannicholas22f939e2016-10-13 13:25:34 -0700694 return nullptr;
695 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700696}
697
Ethan Nicholasfc994162019-06-06 10:04:27 -0400698std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
699 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
John Stilesb61ee902020-09-21 12:26:59 -0400700 return std::make_unique<DiscardStatement>(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700701}
702
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500703std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400704 Layout invokeLayout;
705 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
John Stiles586df952020-11-12 18:27:13 -0500706 const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>(
707 /*offset=*/-1,
708 fModifiers->addToPool(invokeModifiers),
709 "_invoke",
710 std::vector<const Variable*>(),
711 fContext.fVoid_Type.get(),
712 fIsBuiltinCode));
John Stilesb9af7232020-08-20 15:57:48 -0400713 fProgramElements->push_back(std::make_unique<FunctionDefinition>(/*offset=*/-1,
John Stiles607d36b2020-10-19 15:00:01 -0400714 invokeDecl, fIsBuiltinCode,
John Stilesb9af7232020-08-20 15:57:48 -0400715 std::move(main)));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400716
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000717 std::vector<std::unique_ptr<VarDeclaration>> variables;
John Stilesb9af7232020-08-20 15:57:48 -0400718 const Variable* loopIdx = &(*fSymbolTable)["sk_InvocationID"]->as<Variable>();
John Stiles8e3b6be2020-10-13 11:14:08 -0400719 auto test = std::make_unique<BinaryExpression>(/*offset=*/-1,
720 std::make_unique<VariableReference>(/*offset=*/-1, loopIdx),
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400721 Token::Kind::TK_LT,
John Stiles8e3b6be2020-10-13 11:14:08 -0400722 std::make_unique<IntLiteral>(fContext, /*offset=*/-1, fInvocations),
723 fContext.fBool_Type.get());
724 auto next = std::make_unique<PostfixExpression>(
725 std::make_unique<VariableReference>(/*offset=*/-1, loopIdx,
726 VariableReference::RefKind::kReadWrite),
727 Token::Kind::TK_PLUSPLUS);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400728 ASTNode endPrimitiveID(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, "EndPrimitive");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400729 std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400730 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400731
John Stiles8f2a0cf2020-10-13 12:48:21 -0400732 StatementArray loopBody;
John Stilesf4bda742020-10-14 16:57:41 -0400733 loopBody.reserve_back(2);
John Stiles8e3b6be2020-10-13 11:14:08 -0400734 loopBody.push_back(std::make_unique<ExpressionStatement>(this->call(
735 /*offset=*/-1, *invokeDecl,
736 ExpressionArray{})));
737 loopBody.push_back(std::make_unique<ExpressionStatement>(this->call(
738 /*offset=*/-1, std::move(endPrimitive),
739 ExpressionArray{})));
740 auto assignment = std::make_unique<BinaryExpression>(/*offset=*/-1,
741 std::make_unique<VariableReference>(/*offset=*/-1, loopIdx,
742 VariableReference::RefKind::kWrite),
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400743 Token::Kind::TK_EQ,
John Stiles8e3b6be2020-10-13 11:14:08 -0400744 std::make_unique<IntLiteral>(fContext, /*offset=*/-1, /*value=*/0),
745 fContext.fInt_Type.get());
746 auto initializer = std::make_unique<ExpressionStatement>(std::move(assignment));
747 auto loop = std::make_unique<ForStatement>(/*offset=*/-1,
748 std::move(initializer),
749 std::move(test), std::move(next),
750 std::make_unique<Block>(-1, std::move(loopBody)),
751 fSymbolTable);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400752 StatementArray children;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400753 children.push_back(std::move(loop));
John Stilesfbd050b2020-08-03 13:21:46 -0400754 return std::make_unique<Block>(-1, std::move(children));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400755}
756
Robert Phillipsfe8da172018-01-24 14:52:02 +0000757std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
Brian Osman88cda172020-10-09 12:05:16 -0400758 const Variable* skPerVertex = nullptr;
759 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
760 SkASSERT(perVertexDecl->is<InterfaceBlock>());
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400761 skPerVertex = &perVertexDecl->as<InterfaceBlock>().variable();
Brian Osman88cda172020-10-09 12:05:16 -0400762 }
763
Ethan Nicholasb809efb2018-04-12 14:39:21 -0400764 // sk_Position = float4(sk_Position.xy * rtAdjust.xz + sk_Position.ww * rtAdjust.yw,
Robert Phillipsfe8da172018-01-24 14:52:02 +0000765 // 0,
766 // sk_Position.w);
Brian Osman88cda172020-10-09 12:05:16 -0400767 SkASSERT(skPerVertex && fRTAdjust);
John Stiles5acf6a82020-11-10 22:38:01 -0500768 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
769 return std::make_unique<VariableReference>(-1, var, VariableReference::RefKind::kRead);
770 };
771 auto WRef = [](const Variable* var) -> std::unique_ptr<Expression> {
772 return std::make_unique<VariableReference>(-1, var, VariableReference::RefKind::kWrite);
773 };
774 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
775 return std::make_unique<FieldAccess>(Ref(var), idx,
776 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
777 };
778 auto Pos = [&]() -> std::unique_ptr<Expression> {
779 return std::make_unique<FieldAccess>(WRef(skPerVertex), 0,
780 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
781 };
782 auto Adjust = [&]() -> std::unique_ptr<Expression> {
783 return fRTAdjustInterfaceBlock ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
784 : Ref(fRTAdjust);
785 };
786 auto Swizzle = [&](std::unique_ptr<Expression> expr,
787 const ComponentArray& comp) -> std::unique_ptr<Expression> {
788 return std::make_unique<SkSL::Swizzle>(fContext, std::move(expr), comp);
789 };
790 auto Op = [&](std::unique_ptr<Expression> left, Token::Kind op,
791 std::unique_ptr<Expression> right) -> std::unique_ptr<Expression> {
792 return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right),
793 fContext.fFloat2_Type.get());
794 };
John Stiles750109b2020-10-30 13:45:46 -0400795
John Stiles5acf6a82020-11-10 22:38:01 -0500796 static const ComponentArray kXYIndices{0, 1};
797 static const ComponentArray kXZIndices{0, 2};
798 static const ComponentArray kYWIndices{1, 3};
799 static const ComponentArray kWWIndices{3, 3};
800 static const ComponentArray kWIndex{3};
John Stiles750109b2020-10-30 13:45:46 -0400801
John Stiles8e3b6be2020-10-13 11:14:08 -0400802 ExpressionArray children;
John Stilesf4bda742020-10-14 16:57:41 -0400803 children.reserve_back(3);
John Stiles5acf6a82020-11-10 22:38:01 -0500804 children.push_back(Op(
805 Op(Swizzle(Pos(), kXYIndices), Token::Kind::TK_STAR, Swizzle(Adjust(), kXZIndices)),
806 Token::Kind::TK_PLUS,
807 Op(Swizzle(Pos(), kWWIndices), Token::Kind::TK_STAR, Swizzle(Adjust(), kYWIndices))));
John Stiles8e3b6be2020-10-13 11:14:08 -0400808 children.push_back(std::make_unique<FloatLiteral>(fContext, /*offset=*/-1, /*value=*/0.0));
John Stiles5acf6a82020-11-10 22:38:01 -0500809 children.push_back(Swizzle(Pos(), kWIndex));
810 std::unique_ptr<Expression> result = Op(Pos(), Token::Kind::TK_EQ,
John Stiles8e3b6be2020-10-13 11:14:08 -0400811 std::make_unique<Constructor>(/*offset=*/-1,
812 fContext.fFloat4_Type.get(),
813 std::move(children)));
814 return std::make_unique<ExpressionStatement>(std::move(result));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000815}
816
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400817template<typename T>
818class AutoClear {
819public:
820 AutoClear(T* container)
821 : fContainer(container) {
822 SkASSERT(container->empty());
823 }
824
825 ~AutoClear() {
826 fContainer->clear();
827 }
828
829private:
830 T* fContainer;
831};
832
John Stilesb8e010c2020-08-11 18:05:39 -0400833template <typename T> AutoClear(T* c) -> AutoClear<T>;
834
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400835void IRGenerator::checkModifiers(int offset, const Modifiers& modifiers, int permitted) {
836 int flags = modifiers.fFlags;
837 #define CHECK(flag, name) \
838 if (!flags) return; \
839 if (flags & flag) { \
840 if (!(permitted & flag)) { \
841 fErrors.error(offset, "'" name "' is not permitted here"); \
842 } \
843 flags &= ~flag; \
844 }
845 CHECK(Modifiers::kConst_Flag, "const")
846 CHECK(Modifiers::kIn_Flag, "in")
847 CHECK(Modifiers::kOut_Flag, "out")
848 CHECK(Modifiers::kUniform_Flag, "uniform")
849 CHECK(Modifiers::kFlat_Flag, "flat")
850 CHECK(Modifiers::kNoPerspective_Flag, "noperspective")
851 CHECK(Modifiers::kReadOnly_Flag, "readonly")
852 CHECK(Modifiers::kWriteOnly_Flag, "writeonly")
853 CHECK(Modifiers::kCoherent_Flag, "coherent")
854 CHECK(Modifiers::kVolatile_Flag, "volatile")
855 CHECK(Modifiers::kRestrict_Flag, "restrict")
856 CHECK(Modifiers::kBuffer_Flag, "buffer")
857 CHECK(Modifiers::kHasSideEffects_Flag, "sk_has_side_effects")
858 CHECK(Modifiers::kPLS_Flag, "__pixel_localEXT")
859 CHECK(Modifiers::kPLSIn_Flag, "__pixel_local_inEXT")
860 CHECK(Modifiers::kPLSOut_Flag, "__pixel_local_outEXT")
861 CHECK(Modifiers::kVarying_Flag, "varying")
Ethan Nicholasf3c8f5d2020-08-20 13:09:14 +0000862 CHECK(Modifiers::kInline_Flag, "inline")
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400863 SkASSERT(flags == 0);
864}
865
Ethan Nicholasfc994162019-06-06 10:04:27 -0400866void IRGenerator::convertFunction(const ASTNode& f) {
John Stilesb8e010c2020-08-11 18:05:39 -0400867 AutoClear clear(&fReferencedIntrinsics);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400868 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400869 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400870 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400871 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700872 }
John Stiles076e9a22020-12-03 10:37:45 -0500873 auto typeIsAllowed = [&](const Type* t) {
Brian Osman3000d6b2020-07-31 15:57:28 -0400874#if defined(SKSL_STANDALONE)
875 return true;
876#else
877 GrSLType unusedSLType;
878 return fKind != Program::kPipelineStage_Kind ||
879 type_to_grsltype(fContext, *t, &unusedSLType);
880#endif
881 };
882 if (returnType->nonnullable() == *fContext.fFragmentProcessor_Type ||
John Stiles076e9a22020-12-03 10:37:45 -0500883 returnType->typeKind() == Type::TypeKind::kArray ||
884 !typeIsAllowed(returnType)) {
Brian Osman82329002020-07-21 09:39:27 -0400885 fErrors.error(f.fOffset,
886 "functions may not return type '" + returnType->displayName() + "'");
887 return;
888 }
John Stilesb9af7232020-08-20 15:57:48 -0400889 const ASTNode::FunctionData& funcData = f.getFunctionData();
890 this->checkModifiers(f.fOffset, funcData.fModifiers, Modifiers::kHasSideEffects_Flag |
891 Modifiers::kInline_Flag);
Brian Osman5bf3e202020-10-13 10:34:18 -0400892 std::vector<const Variable*> parameters;
John Stilesb9af7232020-08-20 15:57:48 -0400893 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400894 const ASTNode& param = *(iter++);
895 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
896 ASTNode::ParameterData pd = param.getParameterData();
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400897 this->checkModifiers(param.fOffset, pd.fModifiers, Modifiers::kIn_Flag |
898 Modifiers::kOut_Flag);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400899 auto paramIter = param.begin();
900 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700901 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400902 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700903 }
John Stilesd39aec92020-12-03 14:37:16 -0500904 if (pd.fIsArray) {
905 int arraySize = (paramIter++)->getInt();
906 type = fSymbolTable->addArrayDimensions(type, {arraySize});
ethannicholasb3058bd2016-07-01 08:22:01 -0700907 }
Brian Osman3000d6b2020-07-31 15:57:28 -0400908 // Only the (builtin) declarations of 'sample' are allowed to have FP parameters
909 if ((type->nonnullable() == *fContext.fFragmentProcessor_Type && !fIsBuiltinCode) ||
John Stiles076e9a22020-12-03 10:37:45 -0500910 !typeIsAllowed(type)) {
Brian Osman3000d6b2020-07-31 15:57:28 -0400911 fErrors.error(param.fOffset,
912 "parameters of type '" + type->displayName() + "' not allowed");
913 return;
914 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400915
916 Modifiers m = pd.fModifiers;
917 if (funcData.fName == "main" && (fKind == Program::kPipelineStage_Kind ||
918 fKind == Program::kFragmentProcessor_Kind)) {
919 if (i == 0) {
920 // We verify that the type is correct later, for now, if there is a parameter to
921 // a .fp or runtime-effect main(), it's supposed to be the coords:
922 m.fLayout.fBuiltin = SK_MAIN_COORDS_BUILTIN;
923 }
924 }
925
Brian Osman5bf3e202020-10-13 10:34:18 -0400926 const Variable* var = fSymbolTable->takeOwnershipOfSymbol(
John Stiles586df952020-11-12 18:27:13 -0500927 std::make_unique<Variable>(param.fOffset, fModifiers->addToPool(m), pd.fName, type,
Brian Osman8dbdf232020-10-12 14:40:24 -0400928 fIsBuiltinCode, Variable::Storage::kParameter));
ethannicholasd598f792016-07-25 10:08:54 -0700929 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700930 }
931
Brian Osman767f4442020-08-13 16:59:48 -0400932 auto paramIsCoords = [&](int idx) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400933 return parameters[idx]->type() == *fContext.fFloat2_Type &&
Brian Osman8dbdf232020-10-12 14:40:24 -0400934 parameters[idx]->modifiers().fFlags == 0 &&
935 parameters[idx]->modifiers().fLayout.fBuiltin == SK_MAIN_COORDS_BUILTIN;
Brian Osman767f4442020-08-13 16:59:48 -0400936 };
Brian Osman767f4442020-08-13 16:59:48 -0400937
John Stilesb9af7232020-08-20 15:57:48 -0400938 if (funcData.fName == "main") {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400939 switch (fKind) {
940 case Program::kPipelineStage_Kind: {
Brian Osman33316412020-11-06 10:42:51 -0500941 // (half4|float4) main() -or- (half4|float4) main(float2)
942 if (*returnType != *fContext.fHalf4_Type && *returnType != *fContext.fFloat4_Type) {
943 fErrors.error(f.fOffset, "'main' must return: 'vec4', 'float4', or 'half4'");
944 return;
945 }
946 if (!(parameters.size() == 0 || (parameters.size() == 1 && paramIsCoords(0)))) {
947 fErrors.error(f.fOffset, "'main' parameters must be: (), (vec2), or (float2)");
Brian Osman767f4442020-08-13 16:59:48 -0400948 return;
949 }
950 break;
Brian Osman44820a92020-08-26 09:27:39 -0400951 }
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400952 case Program::kFragmentProcessor_Kind: {
Brian Osman44820a92020-08-26 09:27:39 -0400953 bool valid = (parameters.size() == 0) ||
954 (parameters.size() == 1 && paramIsCoords(0));
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400955 if (!valid) {
956 fErrors.error(f.fOffset, ".fp 'main' must be declared main() or main(float2)");
957 return;
958 }
959 break;
960 }
Ethan Nicholas746035a2019-04-23 13:31:09 -0400961 case Program::kGeneric_Kind:
Ethan Nicholas0d997662019-04-08 09:46:01 -0400962 break;
Ethan Nicholas0d997662019-04-08 09:46:01 -0400963 default:
964 if (parameters.size()) {
965 fErrors.error(f.fOffset, "shader 'main' must have zero parameters");
966 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400967 }
968 }
969
ethannicholasb3058bd2016-07-01 08:22:01 -0700970 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700971 const FunctionDeclaration* decl = nullptr;
John Stilesb9af7232020-08-20 15:57:48 -0400972 const Symbol* entry = (*fSymbolTable)[funcData.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700973 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700974 std::vector<const FunctionDeclaration*> functions;
Ethan Nicholase6592142020-09-08 10:22:09 -0400975 switch (entry->kind()) {
976 case Symbol::Kind::kUnresolvedFunction:
Ethan Nicholasceb62142020-10-09 16:51:18 -0400977 functions = entry->as<UnresolvedFunction>().functions();
ethannicholasb3058bd2016-07-01 08:22:01 -0700978 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400979 case Symbol::Kind::kFunctionDeclaration:
John Stiles17c5b702020-08-18 10:40:03 -0400980 functions.push_back(&entry->as<FunctionDeclaration>());
ethannicholasb3058bd2016-07-01 08:22:01 -0700981 break;
982 default:
John Stilesb9af7232020-08-20 15:57:48 -0400983 fErrors.error(f.fOffset, "symbol '" + funcData.fName + "' was already defined");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400984 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700985 }
John Stilesb9af7232020-08-20 15:57:48 -0400986 for (const FunctionDeclaration* other : functions) {
Ethan Nicholase2c49992020-10-05 11:49:11 -0400987 SkASSERT(other->name() == funcData.fName);
Ethan Nicholased84b732020-10-08 11:45:44 -0400988 if (parameters.size() == other->parameters().size()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700989 bool match = true;
990 for (size_t i = 0; i < parameters.size(); i++) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400991 if (parameters[i]->type() != other->parameters()[i]->type()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700992 match = false;
993 break;
994 }
995 }
996 if (match) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400997 if (*returnType != other->returnType()) {
998 FunctionDeclaration newDecl(f.fOffset,
John Stiles586df952020-11-12 18:27:13 -0500999 fModifiers->addToPool(funcData.fModifiers),
Ethan Nicholased84b732020-10-08 11:45:44 -04001000 funcData.fName,
1001 parameters,
1002 returnType,
1003 fIsBuiltinCode);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001004 fErrors.error(f.fOffset, "functions '" + newDecl.description() +
1005 "' and '" + other->description() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001006 "' differ only in return type");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001007 return;
ethannicholasb3058bd2016-07-01 08:22:01 -07001008 }
1009 decl = other;
1010 for (size_t i = 0; i < parameters.size(); i++) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001011 if (parameters[i]->modifiers() != other->parameters()[i]->modifiers()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001012 fErrors.error(f.fOffset, "modifiers on parameter " +
1013 to_string((uint64_t) i + 1) +
John Stilesb9af7232020-08-20 15:57:48 -04001014 " differ between declaration and definition");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001015 return;
ethannicholasb3058bd2016-07-01 08:22:01 -07001016 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001017 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001018 if (other->definition() && !other->isBuiltin()) {
John Stilesb9af7232020-08-20 15:57:48 -04001019 fErrors.error(f.fOffset, "duplicate definition of " + other->description());
John Stilesbfce87b2020-11-12 16:03:23 -05001020 return;
ethannicholasb3058bd2016-07-01 08:22:01 -07001021 }
1022 break;
1023 }
1024 }
1025 }
1026 }
1027 if (!decl) {
John Stilesb9af7232020-08-20 15:57:48 -04001028 // Conservatively assume all user-defined functions have side effects.
1029 Modifiers declModifiers = funcData.fModifiers;
1030 if (!fIsBuiltinCode) {
1031 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
1032 }
1033
1034 // Create a new declaration.
John Stiles586df952020-11-12 18:27:13 -05001035 decl = fSymbolTable->add(
1036 std::make_unique<FunctionDeclaration>(f.fOffset,
1037 fModifiers->addToPool(declModifiers),
1038 funcData.fName,
1039 parameters,
1040 returnType,
1041 fIsBuiltinCode));
ethannicholasb3058bd2016-07-01 08:22:01 -07001042 }
John Stiles569249b2020-11-03 12:18:22 -05001043 if (iter == f.end()) {
1044 // If there's no body, we've found a prototype.
1045 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
1046 fIsBuiltinCode));
1047 } else {
1048 // Compile function body.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001049 SkASSERT(!fCurrentFunction);
ethannicholasd598f792016-07-25 10:08:54 -07001050 fCurrentFunction = decl;
John Stiles569249b2020-11-03 12:18:22 -05001051
ethannicholasd598f792016-07-25 10:08:54 -07001052 AutoSymbolTable table(this);
John Stiles569249b2020-11-03 12:18:22 -05001053 for (const Variable* param : decl->parameters()) {
1054 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -07001055 }
John Stilesb9af7232020-08-20 15:57:48 -04001056 bool needInvocationIDWorkaround = fInvocations != -1 && funcData.fName == "main" &&
Brian Osmand7e76592020-11-02 12:26:22 -05001057 fCaps && !fCaps->gsInvocationsSupport();
Ethan Nicholasfc994162019-06-06 10:04:27 -04001058 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -07001059 fCurrentFunction = nullptr;
1060 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001061 return;
1062 }
1063 if (needInvocationIDWorkaround) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001064 body = this->applyInvocationIDWorkaround(std::move(body));
ethannicholasd598f792016-07-25 10:08:54 -07001065 }
John Stilesb9af7232020-08-20 15:57:48 -04001066 if (Program::kVertex_Kind == fKind && funcData.fName == "main" && fRTAdjust) {
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001067 body->children().push_back(this->getNormalizeSkPositionCode());
Robert Phillipsfe8da172018-01-24 14:52:02 +00001068 }
John Stiles607d36b2020-10-19 15:00:01 -04001069 auto result = std::make_unique<FunctionDefinition>(
1070 f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics));
Ethan Nicholased84b732020-10-08 11:45:44 -04001071 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001072 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001073 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -07001074 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001075}
1076
John Stilesdc75a972020-11-25 16:24:55 -05001077std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
1078 SkASSERT(node.fKind == ASTNode::Kind::kType);
1079
1080 const Type* type = this->convertType(node);
1081 if (!type) {
1082 return nullptr;
1083 }
1084 if (type->typeKind() != Type::TypeKind::kStruct) {
1085 fErrors.error(node.fOffset, "expected a struct here, found '" + type->name() + "'");
1086 return nullptr;
1087 }
1088 return std::make_unique<StructDefinition>(node.fOffset, *type);
1089}
1090
Ethan Nicholasfc994162019-06-06 10:04:27 -04001091std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
Brian Osman16f376f2020-09-02 12:30:59 -04001092 if (fKind != Program::kFragment_Kind &&
1093 fKind != Program::kVertex_Kind &&
1094 fKind != Program::kGeometry_Kind) {
1095 fErrors.error(intf.fOffset, "interface block is not allowed here");
1096 return nullptr;
1097 }
1098
Ethan Nicholasfc994162019-06-06 10:04:27 -04001099 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
1100 ASTNode::InterfaceBlockData id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -07001101 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -04001102 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -07001103 std::vector<Type::Field> fields;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001104 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001105 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -04001106 {
1107 AutoSymbolTable table(this);
1108 symbols = fSymbolTable;
1109 bool haveRuntimeArray = false;
1110 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
1111 StatementArray decls = this->convertVarDeclarations(*(iter++),
1112 Variable::Storage::kInterfaceBlock);
1113 if (decls.empty()) {
1114 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001115 }
John Stiles869cdef2020-10-30 14:24:24 -04001116 for (const auto& decl : decls) {
1117 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles1d757782020-11-17 09:43:22 -05001118 if (vd.var().type().isOpaque()) {
1119 fErrors.error(decl->fOffset, "opaque type '" + vd.var().type().name() +
1120 "' is not permitted in an interface block");
1121 }
John Stiles869cdef2020-10-30 14:24:24 -04001122 if (haveRuntimeArray) {
1123 fErrors.error(decl->fOffset,
1124 "only the last entry in an interface block may be a runtime-sized "
1125 "array");
1126 }
1127 if (&vd.var() == fRTAdjust) {
1128 foundRTAdjust = true;
1129 SkASSERT(vd.var().type() == *fContext.fFloat4_Type);
1130 fRTAdjustFieldIndex = fields.size();
1131 }
1132 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
1133 &vd.var().type()));
1134 if (vd.value()) {
1135 fErrors.error(decl->fOffset,
1136 "initializers are not permitted on interface block fields");
1137 }
1138 if (vd.var().type().typeKind() == Type::TypeKind::kArray &&
1139 vd.var().type().columns() == Type::kUnsizedArray) {
1140 haveRuntimeArray = true;
1141 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04001142 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001143 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001144 }
John Stilesd39aec02020-12-03 10:42:26 -05001145 const Type* type = old->takeOwnershipOfSymbol(std::make_unique<Type>(intf.fOffset, id.fTypeName,
1146 fields));
1147 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -05001148 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001149 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001150 if (size) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001151 std::unique_ptr<Expression> converted = this->convertExpression(size);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001152 if (!converted) {
1153 return nullptr;
1154 }
John Stilesd39aec02020-12-03 10:42:26 -05001155 if (!converted->is<IntLiteral>()) {
Ethan Nicholas66d80062019-09-09 14:50:51 -04001156 fErrors.error(intf.fOffset, "array size must be specified");
1157 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001158 }
John Stilesd39aec02020-12-03 10:42:26 -05001159 arraySize = converted->as<IntLiteral>().value();
1160 if (arraySize <= 0) {
1161 fErrors.error(converted->fOffset, "array size must be positive");
1162 return nullptr;
1163 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001164 } else {
John Stilesd39aec02020-12-03 10:42:26 -05001165 arraySize = Type::kUnsizedArray;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001166 }
John Stilesd39aec02020-12-03 10:42:26 -05001167 type = symbols->addArrayDimensions(type, {arraySize});
Ethan Nicholas50afc172017-02-16 14:49:57 -05001168 }
Brian Osman5bf3e202020-10-13 10:34:18 -04001169 const Variable* var = old->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -04001170 std::make_unique<Variable>(intf.fOffset,
John Stiles586df952020-11-12 18:27:13 -05001171 fModifiers->addToPool(id.fModifiers),
John Stiles3ae071e2020-08-05 15:29:29 -04001172 id.fInstanceName.fLength ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001173 type,
Brian Osman3887a012020-09-30 13:22:27 -04001174 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001175 Variable::Storage::kGlobal));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001176 if (foundRTAdjust) {
1177 fRTAdjustInterfaceBlock = var;
1178 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001179 if (id.fInstanceName.fLength) {
John Stilesb8cc6652020-10-08 09:12:07 -04001180 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001181 } else {
1182 for (size_t i = 0; i < fields.size(); i++) {
John Stilesb8cc6652020-10-08 09:12:07 -04001183 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001184 }
1185 }
John Stilesfbd050b2020-08-03 13:21:46 -04001186 return std::make_unique<InterfaceBlock>(intf.fOffset,
1187 var,
1188 id.fTypeName,
1189 id.fInstanceName,
John Stilesd39aec02020-12-03 10:42:26 -05001190 arraySize,
John Stilesfbd050b2020-08-03 13:21:46 -04001191 symbols);
ethannicholasb3058bd2016-07-01 08:22:01 -07001192}
1193
Brian Osman3e3db6c2020-08-14 09:42:12 -04001194bool IRGenerator::getConstantInt(const Expression& value, int64_t* out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001195 switch (value.kind()) {
1196 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -04001197 *out = value.as<IntLiteral>().value();
Brian Osman3e3db6c2020-08-14 09:42:12 -04001198 return true;
Ethan Nicholase6592142020-09-08 10:22:09 -04001199 case Expression::Kind::kVariableReference: {
Ethan Nicholas78686922020-10-08 06:46:27 -04001200 const Variable& var = *value.as<VariableReference>().variable();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001201 return (var.modifiers().fFlags & Modifiers::kConst_Flag) &&
1202 var.initialValue() && this->getConstantInt(*var.initialValue(), out);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001203 }
1204 default:
Brian Osman3e3db6c2020-08-14 09:42:12 -04001205 return false;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001206 }
1207}
1208
John Stiles7bd70332020-11-30 17:04:09 -05001209void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
1210 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
1211 for (std::unique_ptr<Statement>& stmt : decls) {
1212 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(decl.fOffset,
1213 std::move(stmt)));
1214 }
1215}
1216
Ethan Nicholasfc994162019-06-06 10:04:27 -04001217void IRGenerator::convertEnum(const ASTNode& e) {
Brian Osman16f376f2020-09-02 12:30:59 -04001218 if (fKind == Program::kPipelineStage_Kind) {
1219 fErrors.error(e.fOffset, "enum is not allowed here");
1220 return;
1221 }
1222
Ethan Nicholasfc994162019-06-06 10:04:27 -04001223 SkASSERT(e.fKind == ASTNode::Kind::kEnum);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001224 int64_t currentValue = 0;
1225 Layout layout;
John Stilesdc75a972020-11-25 16:24:55 -05001226 ASTNode enumType(
1227 e.fNodes, e.fOffset, ASTNode::Kind::kType,
1228 ASTNode::TypeData(e.getString(), /*isStructDeclaration=*/false, /*isNullable=*/false));
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001229 const Type* type = this->convertType(enumType);
1230 Modifiers modifiers(layout, Modifiers::kConst_Flag);
Brian Osman1313d1a2020-09-08 10:34:30 -04001231 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -04001232 fSymbolTable = std::make_shared<SymbolTable>(fSymbolTable, fIsBuiltinCode);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001233 for (auto iter = e.begin(); iter != e.end(); ++iter) {
1234 const ASTNode& child = *iter;
1235 SkASSERT(child.fKind == ASTNode::Kind::kEnumCase);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001236 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001237 if (child.begin() != child.end()) {
1238 value = this->convertExpression(*child.begin());
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001239 if (!value) {
Brian Osman1313d1a2020-09-08 10:34:30 -04001240 fSymbolTable = oldTable;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001241 return;
1242 }
Brian Osman3e3db6c2020-08-14 09:42:12 -04001243 if (!this->getConstantInt(*value, &currentValue)) {
1244 fErrors.error(value->fOffset, "enum value must be a constant integer");
Brian Osman1313d1a2020-09-08 10:34:30 -04001245 fSymbolTable = oldTable;
Brian Osman3e3db6c2020-08-14 09:42:12 -04001246 return;
1247 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001248 }
John Stilese1bbd5c2020-11-17 11:33:49 -05001249 value = std::make_unique<IntLiteral>(fContext, e.fOffset, currentValue);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001250 ++currentValue;
John Stiles586df952020-11-12 18:27:13 -05001251 fSymbolTable->add(std::make_unique<Variable>(e.fOffset, fModifiers->addToPool(modifiers),
John Stilesb8cc6652020-10-08 09:12:07 -04001252 child.getString(), type, fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001253 Variable::Storage::kGlobal, value.get()));
Brian Osman3e3db6c2020-08-14 09:42:12 -04001254 fSymbolTable->takeOwnershipOfIRNode(std::move(value));
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001255 }
Brian Osman1313d1a2020-09-08 10:34:30 -04001256 // Now we orphanize the Enum's symbol table, so that future lookups in it are strict
1257 fSymbolTable->fParent = nullptr;
John Stiles1c823672020-10-20 10:23:50 -04001258 fProgramElements->push_back(std::make_unique<Enum>(e.fOffset, e.getString(), fSymbolTable,
1259 /*isSharedWithCpp=*/fIsBuiltinCode,
1260 /*isBuiltin=*/fIsBuiltinCode));
Brian Osman1313d1a2020-09-08 10:34:30 -04001261 fSymbolTable = oldTable;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001262}
1263
John Stilesb4b627e2020-11-13 15:55:27 -05001264bool IRGenerator::typeContainsPrivateFields(const Type& type) {
1265 // Checks for usage of private types, including fields inside a struct.
1266 if (type.isPrivate()) {
1267 return true;
1268 }
1269 if (type.typeKind() == Type::TypeKind::kStruct) {
1270 for (const auto& f : type.fields()) {
1271 if (this->typeContainsPrivateFields(*f.fType)) {
1272 return true;
1273 }
1274 }
1275 }
1276 return false;
1277}
1278
Brian Osmand8070392020-09-09 15:50:02 -04001279const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001280 ASTNode::TypeData td = type.getTypeData();
John Stiles6bef6a72020-12-02 14:26:04 -05001281 const Symbol* symbol = (*fSymbolTable)[td.fName];
1282 if (!symbol || !symbol->is<Type>()) {
John Stiles7bd70332020-11-30 17:04:09 -05001283 fErrors.error(type.fOffset, "unknown type '" + td.fName + "'");
1284 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001285 }
John Stiles6bef6a72020-12-02 14:26:04 -05001286 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -05001287 const bool isArray = (type.begin() != type.end());
1288 if (td.fIsNullable) {
John Stiles6bef6a72020-12-02 14:26:04 -05001289 if (*result == *fContext.fFragmentProcessor_Type) {
John Stiles7bd70332020-11-30 17:04:09 -05001290 if (isArray) {
1291 fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in "
1292 "an array");
1293 }
1294 result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Type>(
John Stiles6bef6a72020-12-02 14:26:04 -05001295 String(result->name()) + "?", Type::TypeKind::kNullable, *result));
John Stiles7bd70332020-11-30 17:04:09 -05001296 } else {
1297 fErrors.error(type.fOffset, "type '" + td.fName + "' may not be nullable");
1298 }
1299 }
John Stiles6bef6a72020-12-02 14:26:04 -05001300 if (*result == *fContext.fVoid_Type) {
John Stiles7bd70332020-11-30 17:04:09 -05001301 if (!allowVoid) {
1302 fErrors.error(type.fOffset, "type '" + td.fName + "' not allowed in this context");
1303 return nullptr;
1304 }
1305 if (isArray) {
1306 fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in an array");
1307 return nullptr;
1308 }
1309 }
John Stiles6bef6a72020-12-02 14:26:04 -05001310 if (!fIsBuiltinCode && this->typeContainsPrivateFields(*result)) {
John Stiles7bd70332020-11-30 17:04:09 -05001311 fErrors.error(type.fOffset, "type '" + td.fName + "' is private");
1312 return nullptr;
1313 }
John Stiles6bef6a72020-12-02 14:26:04 -05001314 if (isArray && result->isOpaque()) {
John Stiles7bd70332020-11-30 17:04:09 -05001315 fErrors.error(type.fOffset,
1316 "opaque type '" + td.fName + "' may not be used in an array");
1317 return nullptr;
1318 }
John Stiles6bef6a72020-12-02 14:26:04 -05001319 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -05001320 auto iter = type.begin();
1321 int arraySize = *iter ? iter->getInt() : Type::kUnsizedArray;
1322 result = fSymbolTable->addArrayDimensions(result, {arraySize});
John Stiles7bd70332020-11-30 17:04:09 -05001323 }
John Stiles6bef6a72020-12-02 14:26:04 -05001324 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001325}
1326
Ethan Nicholasfc994162019-06-06 10:04:27 -04001327std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001328 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001329 case ASTNode::Kind::kBinary:
1330 return this->convertBinaryExpression(expr);
1331 case ASTNode::Kind::kBool:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001332 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001333 expr.getBool()));
1334 case ASTNode::Kind::kCall:
1335 return this->convertCallExpression(expr);
1336 case ASTNode::Kind::kField:
1337 return this->convertFieldExpression(expr);
1338 case ASTNode::Kind::kFloat:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001339 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001340 expr.getFloat()));
1341 case ASTNode::Kind::kIdentifier:
1342 return this->convertIdentifier(expr);
1343 case ASTNode::Kind::kIndex:
1344 return this->convertIndexExpression(expr);
1345 case ASTNode::Kind::kInt:
1346 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset,
1347 expr.getInt()));
1348 case ASTNode::Kind::kNull:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001349 return std::unique_ptr<Expression>(new NullLiteral(fContext, expr.fOffset));
Ethan Nicholasfc994162019-06-06 10:04:27 -04001350 case ASTNode::Kind::kPostfix:
1351 return this->convertPostfixExpression(expr);
1352 case ASTNode::Kind::kPrefix:
1353 return this->convertPrefixExpression(expr);
Brian Osman6518d772020-09-10 16:50:06 -04001354 case ASTNode::Kind::kScope:
1355 return this->convertScopeExpression(expr);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001356 case ASTNode::Kind::kTernary:
1357 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001358 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001359#ifdef SK_DEBUG
Ethan Nicholasfc994162019-06-06 10:04:27 -04001360 ABORT("unsupported expression: %s\n", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001361#endif
1362 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001363 }
1364}
1365
Ethan Nicholasfc994162019-06-06 10:04:27 -04001366std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
1367 SkASSERT(identifier.fKind == ASTNode::Kind::kIdentifier);
1368 const Symbol* result = (*fSymbolTable)[identifier.getString()];
ethannicholasb3058bd2016-07-01 08:22:01 -07001369 if (!result) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001370 fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.getString() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001371 return nullptr;
1372 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001373 switch (result->kind()) {
1374 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -07001375 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -04001376 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -07001377 };
John Stilesfbd050b2020-08-03 13:21:46 -04001378 return std::make_unique<FunctionReference>(fContext, identifier.fOffset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -07001379 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001380 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -04001381 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Ethan Nicholasceb62142020-10-09 16:51:18 -04001382 return std::make_unique<FunctionReference>(fContext, identifier.fOffset,
1383 f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -07001384 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001385 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -04001386 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001387 const Modifiers& modifiers = var->modifiers();
1388 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -04001389 case SK_WIDTH_BUILTIN:
1390 fInputs.fRTWidth = true;
1391 break;
1392 case SK_HEIGHT_BUILTIN:
Greg Daniele6ab9982018-08-22 13:56:32 +00001393 fInputs.fRTHeight = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001394 break;
1395#ifndef SKSL_STANDALONE
1396 case SK_FRAGCOORD_BUILTIN:
Brian Osman9f313b62019-10-02 12:03:11 -04001397 fInputs.fFlipY = true;
1398 if (fSettings->fFlipY &&
Brian Osmand7e76592020-11-02 12:26:22 -05001399 (!fCaps || !fCaps->fragCoordConventionsExtensionString())) {
Brian Osman9f313b62019-10-02 12:03:11 -04001400 fInputs.fRTHeight = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001401 }
Greg Daniele6ab9982018-08-22 13:56:32 +00001402#endif
Ethan Nicholascd700e92018-08-24 16:43:57 -04001403 }
Ethan Nicholas33c59ed2019-08-13 10:21:38 -04001404 if (fKind == Program::kFragmentProcessor_Kind &&
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001405 (modifiers.fFlags & Modifiers::kIn_Flag) &&
1406 !(modifiers.fFlags & Modifiers::kUniform_Flag) &&
1407 !modifiers.fLayout.fKey &&
1408 modifiers.fLayout.fBuiltin == -1 &&
Ethan Nicholas30d30222020-09-11 12:27:26 -04001409 var->type().nonnullable() != *fContext.fFragmentProcessor_Type &&
1410 var->type().typeKind() != Type::TypeKind::kSampler) {
Ethan Nicholas33c59ed2019-08-13 10:21:38 -04001411 bool valid = false;
1412 for (const auto& decl : fFile->root()) {
1413 if (decl.fKind == ASTNode::Kind::kSection) {
1414 ASTNode::SectionData section = decl.getSectionData();
1415 if (section.fName == "setData") {
1416 valid = true;
1417 break;
1418 }
1419 }
1420 }
1421 if (!valid) {
1422 fErrors.error(identifier.fOffset, "'in' variable must be either 'uniform' or "
1423 "'layout(key)', or there must be a custom "
1424 "@setData function");
1425 }
1426 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001427 // default to kRead_RefKind; this will be corrected later if the variable is written to
John Stilesfbd050b2020-08-03 13:21:46 -04001428 return std::make_unique<VariableReference>(identifier.fOffset,
Brian Osman79457ef2020-09-24 15:01:27 -04001429 var,
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001430 VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -07001431 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001432 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -04001433 const Field* field = &result->as<Field>();
Brian Osman6a204db2020-10-08 09:29:02 -04001434 auto base = std::make_unique<VariableReference>(identifier.fOffset, &field->owner(),
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001435 VariableReference::RefKind::kRead);
Brian Osman6a204db2020-10-08 09:29:02 -04001436 return std::make_unique<FieldAccess>(std::move(base),
1437 field->fieldIndex(),
Ethan Nicholas7a95b202020-10-09 11:55:40 -04001438 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -07001439 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001440 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -04001441 const Type* t = &result->as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -04001442 return std::make_unique<TypeReference>(fContext, identifier.fOffset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -07001443 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001444 case Symbol::Kind::kExternal: {
John Stiles17c5b702020-08-18 10:40:03 -04001445 const ExternalValue* r = &result->as<ExternalValue>();
John Stilesfbd050b2020-08-03 13:21:46 -04001446 return std::make_unique<ExternalValueReference>(identifier.fOffset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -04001447 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001448 default:
Ethan Nicholase6592142020-09-08 10:22:09 -04001449 ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -07001450 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001451}
1452
Ethan Nicholasfc994162019-06-06 10:04:27 -04001453std::unique_ptr<Section> IRGenerator::convertSection(const ASTNode& s) {
Brian Osman16f376f2020-09-02 12:30:59 -04001454 if (fKind != Program::kFragmentProcessor_Kind) {
1455 fErrors.error(s.fOffset, "syntax error");
1456 return nullptr;
1457 }
1458
Ethan Nicholasfc994162019-06-06 10:04:27 -04001459 ASTNode::SectionData section = s.getSectionData();
John Stilesfbd050b2020-08-03 13:21:46 -04001460 return std::make_unique<Section>(s.fOffset, section.fName, section.fArgument,
1461 section.fText);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001462}
1463
Ethan Nicholas11d53972016-11-28 11:23:23 -05001464std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001465 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001466 if (!expr) {
1467 return nullptr;
1468 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001469 if (expr->type() == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001470 return expr;
1471 }
1472 this->checkValid(*expr);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001473 if (expr->type() == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001474 return nullptr;
1475 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001476 if (!expr->coercionCost(type).isPossible(fSettings->fAllowNarrowingConversions)) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001477 fErrors.error(expr->fOffset, "expected '" + type.displayName() + "', but found '" +
Ethan Nicholas30d30222020-09-11 12:27:26 -04001478 expr->type().displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001479 return nullptr;
1480 }
John Stiles9aeed132020-11-24 17:36:06 -05001481 if (type.isScalar()) {
John Stiles8e3b6be2020-10-13 11:14:08 -04001482 ExpressionArray args;
ethannicholasb3058bd2016-07-01 08:22:01 -07001483 args.push_back(std::move(expr));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001484 std::unique_ptr<Expression> ctor;
1485 if (type == *fContext.fFloatLiteral_Type) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001486 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
1487 "float"));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001488 } else if (type == *fContext.fIntLiteral_Type) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001489 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
1490 "int"));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001491 } else {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001492 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
Ethan Nicholase2c49992020-10-05 11:49:11 -04001493 type.name()));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001494 }
1495 if (!ctor) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04001496 printf("error, null identifier: %s\n", String(type.name()).c_str());
Ethan Nicholase1f55022019-02-05 17:17:40 -05001497 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001498 SkASSERT(ctor);
John Stiles8e3b6be2020-10-13 11:14:08 -04001499 return this->call(/*offset=*/-1, std::move(ctor), std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001500 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001501 if (expr->kind() == Expression::Kind::kNullLiteral) {
1502 SkASSERT(type.typeKind() == Type::TypeKind::kNullable);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001503 return std::unique_ptr<Expression>(new NullLiteral(expr->fOffset, &type));
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001504 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001505 ExpressionArray args;
ethannicholas5961bc92016-10-12 06:39:56 -07001506 args.push_back(std::move(expr));
John Stiles8e3b6be2020-10-13 11:14:08 -04001507 return std::make_unique<Constructor>(/*offset=*/-1, &type, std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001508}
1509
John Stiles56b1b802020-11-25 10:21:55 -05001510static bool is_matrix_multiply(const Type& left, Token::Kind op, const Type& right) {
1511 if (op != Token::Kind::TK_STAR && op != Token::Kind::TK_STAREQ) {
1512 return false;
1513 }
John Stiles9aeed132020-11-24 17:36:06 -05001514 if (left.isMatrix()) {
1515 return right.isMatrix() || right.isVector();
ethannicholasf789b382016-08-03 12:43:36 -07001516 }
John Stiles9aeed132020-11-24 17:36:06 -05001517 return left.isVector() && right.isMatrix();
ethannicholasf789b382016-08-03 12:43:36 -07001518}
ethannicholasea4567c2016-10-17 11:24:37 -07001519
ethannicholasb3058bd2016-07-01 08:22:01 -07001520/**
John Stiles56b1b802020-11-25 10:21:55 -05001521 * Defines the set of logical (comparison) operators.
1522 */
1523static bool op_is_logical(Token::Kind op) {
1524 switch (op) {
1525 case Token::Kind::TK_LT:
1526 case Token::Kind::TK_GT:
1527 case Token::Kind::TK_LTEQ:
1528 case Token::Kind::TK_GTEQ:
1529 return true;
1530 default:
1531 return false;
1532 }
1533}
1534
1535/**
1536 * Defines the set of operators which perform bitwise math.
1537 */
1538static bool op_is_bitwise(Token::Kind op) {
1539 switch (op) {
1540 case Token::Kind::TK_SHL:
1541 case Token::Kind::TK_SHR:
1542 case Token::Kind::TK_BITWISEAND:
1543 case Token::Kind::TK_BITWISEOR:
1544 case Token::Kind::TK_BITWISEXOR:
1545 case Token::Kind::TK_SHLEQ:
1546 case Token::Kind::TK_SHREQ:
1547 case Token::Kind::TK_BITWISEANDEQ:
1548 case Token::Kind::TK_BITWISEOREQ:
1549 case Token::Kind::TK_BITWISEXOREQ:
1550 return true;
1551 default:
1552 return false;
1553 }
1554}
1555
1556/**
1557 * Defines the set of operators which perform vector/matrix math.
1558 */
1559static bool op_valid_for_matrix_or_vector(Token::Kind op) {
1560 switch (op) {
1561 case Token::Kind::TK_PLUS:
1562 case Token::Kind::TK_MINUS:
1563 case Token::Kind::TK_STAR:
1564 case Token::Kind::TK_SLASH:
1565 case Token::Kind::TK_PERCENT:
1566 case Token::Kind::TK_SHL:
1567 case Token::Kind::TK_SHR:
1568 case Token::Kind::TK_BITWISEAND:
1569 case Token::Kind::TK_BITWISEOR:
1570 case Token::Kind::TK_BITWISEXOR:
1571 case Token::Kind::TK_PLUSEQ:
1572 case Token::Kind::TK_MINUSEQ:
1573 case Token::Kind::TK_STAREQ:
1574 case Token::Kind::TK_SLASHEQ:
1575 case Token::Kind::TK_PERCENTEQ:
1576 case Token::Kind::TK_SHLEQ:
1577 case Token::Kind::TK_SHREQ:
1578 case Token::Kind::TK_BITWISEANDEQ:
1579 case Token::Kind::TK_BITWISEOREQ:
1580 case Token::Kind::TK_BITWISEXOREQ:
1581 return true;
1582 default:
1583 return false;
1584 }
1585}
1586
1587/**
ethannicholasb3058bd2016-07-01 08:22:01 -07001588 * Determines the operand and result types of a binary expression. Returns true if the expression is
1589 * legal, false otherwise. If false, the values of the out parameters are undefined.
1590 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001591static bool determine_binary_type(const Context& context,
Brian Osman0acb5b52020-09-02 13:45:47 -04001592 bool allowNarrowing,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001593 Token::Kind op,
1594 const Type& left,
1595 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -07001596 const Type** outLeftType,
1597 const Type** outRightType,
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001598 const Type** outResultType) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001599 switch (op) {
John Stiles56b1b802020-11-25 10:21:55 -05001600 case Token::Kind::TK_EQ: // left = right
ethannicholasea4567c2016-10-17 11:24:37 -07001601 *outLeftType = &left;
1602 *outRightType = &left;
1603 *outResultType = &left;
Brian Osman0acb5b52020-09-02 13:45:47 -04001604 return right.canCoerceTo(left, allowNarrowing);
John Stiles56b1b802020-11-25 10:21:55 -05001605
1606 case Token::Kind::TK_EQEQ: // left == right
1607 case Token::Kind::TK_NEQ: { // left != right
Brian Osman0acb5b52020-09-02 13:45:47 -04001608 CoercionCost rightToLeft = right.coercionCost(left),
1609 leftToRight = left.coercionCost(right);
1610 if (rightToLeft < leftToRight) {
1611 if (rightToLeft.isPossible(allowNarrowing)) {
1612 *outLeftType = &left;
1613 *outRightType = &left;
1614 *outResultType = context.fBool_Type.get();
1615 return true;
1616 }
1617 } else {
1618 if (leftToRight.isPossible(allowNarrowing)) {
1619 *outLeftType = &right;
1620 *outRightType = &right;
1621 *outResultType = context.fBool_Type.get();
1622 return true;
1623 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001624 }
Ethan Nicholas23463002018-03-28 15:16:15 -04001625 return false;
Brian Osman0acb5b52020-09-02 13:45:47 -04001626 }
John Stiles56b1b802020-11-25 10:21:55 -05001627 case Token::Kind::TK_LOGICALOR: // left || right
1628 case Token::Kind::TK_LOGICALAND: // left && right
1629 case Token::Kind::TK_LOGICALXOR: // left ^^ right
ethannicholasd598f792016-07-25 10:08:54 -07001630 *outLeftType = context.fBool_Type.get();
1631 *outRightType = context.fBool_Type.get();
1632 *outResultType = context.fBool_Type.get();
Brian Osman0acb5b52020-09-02 13:45:47 -04001633 return left.canCoerceTo(*context.fBool_Type, allowNarrowing) &&
1634 right.canCoerceTo(*context.fBool_Type, allowNarrowing);
John Stiles56b1b802020-11-25 10:21:55 -05001635
1636 case Token::Kind::TK_COMMA: // left, right
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001637 *outLeftType = &left;
1638 *outRightType = &right;
1639 *outResultType = &right;
1640 return true;
John Stiles56b1b802020-11-25 10:21:55 -05001641
ethannicholasb3058bd2016-07-01 08:22:01 -07001642 default:
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001643 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001644 }
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001645
John Stiles56b1b802020-11-25 10:21:55 -05001646 // Boolean types only support the operators listed above (, = == != || && ^^).
1647 // If we've gotten this far with a boolean, we have an unsupported operator.
1648 const Type& leftComponentType(left.columns() > 1 ? left.componentType() : left);
1649 const Type& rightComponentType(right.columns() > 1 ? right.componentType() : right);
1650 if (leftComponentType.isBoolean() || rightComponentType.isBoolean()) {
1651 return false;
1652 }
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001653
John Stiles56b1b802020-11-25 10:21:55 -05001654 bool isAssignment = Compiler::IsAssignment(op);
1655 if (is_matrix_multiply(left, op, right)) { // left * right
1656 // Determine final component type.
1657 if (!determine_binary_type(context, allowNarrowing, op,
1658 left.componentType(), right.componentType(),
1659 outLeftType, outRightType, outResultType)) {
1660 return false;
1661 }
1662 *outLeftType = &(*outResultType)->toCompound(context, left.columns(), left.rows());
1663 *outRightType = &(*outResultType)->toCompound(context, right.columns(), right.rows());
1664 int leftColumns = left.columns(), leftRows = left.rows();
1665 int rightColumns = right.columns(), rightRows = right.rows();
1666 if (right.isVector()) {
1667 // `matrix * vector` treats the vector as a column vector; we need to transpose it.
1668 std::swap(rightColumns, rightRows);
1669 SkASSERT(rightColumns == 1);
1670 }
1671 if (rightColumns > 1) {
1672 *outResultType = &(*outResultType)->toCompound(context, rightColumns, leftRows);
1673 } else {
1674 // The result was a column vector. Transpose it back to a row.
1675 *outResultType = &(*outResultType)->toCompound(context, leftRows, rightColumns);
1676 }
1677 if (isAssignment && ((*outResultType)->columns() != leftColumns ||
1678 (*outResultType)->rows() != leftRows)) {
1679 return false;
1680 }
1681 return leftColumns == rightRows;
1682 }
1683
1684 bool leftIsVectorOrMatrix = left.isVector() || left.isMatrix();
1685 bool validMatrixOrVectorOp = op_valid_for_matrix_or_vector(op);
1686
1687 if (leftIsVectorOrMatrix && validMatrixOrVectorOp && right.isScalar()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001688 if (determine_binary_type(context, allowNarrowing, op, left.componentType(), right,
1689 outLeftType, outRightType, outResultType)) {
ethannicholasd598f792016-07-25 10:08:54 -07001690 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
John Stiles56b1b802020-11-25 10:21:55 -05001691 if (!op_is_logical(op)) {
1692 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
1693 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001694 }
1695 return true;
1696 }
1697 return false;
1698 }
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001699
John Stiles56b1b802020-11-25 10:21:55 -05001700 bool rightIsVectorOrMatrix = right.isVector() || right.isMatrix();
1701
1702 if (!isAssignment && rightIsVectorOrMatrix && validMatrixOrVectorOp && left.isScalar()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001703 if (determine_binary_type(context, allowNarrowing, op, left, right.componentType(),
1704 outLeftType, outRightType, outResultType)) {
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001705 *outRightType = &(*outRightType)->toCompound(context, right.columns(), right.rows());
John Stiles56b1b802020-11-25 10:21:55 -05001706 if (!op_is_logical(op)) {
1707 *outResultType = &(*outResultType)->toCompound(context, right.columns(),
1708 right.rows());
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001709 }
1710 return true;
1711 }
1712 return false;
1713 }
1714
Brian Osman0acb5b52020-09-02 13:45:47 -04001715 CoercionCost rightToLeftCost = right.coercionCost(left);
1716 CoercionCost leftToRightCost = isAssignment ? CoercionCost::Impossible()
1717 : left.coercionCost(right);
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001718
John Stiles9aeed132020-11-24 17:36:06 -05001719 if ((left.isScalar() && right.isScalar()) || (leftIsVectorOrMatrix && validMatrixOrVectorOp)) {
John Stiles56b1b802020-11-25 10:21:55 -05001720 if (op_is_bitwise(op)) {
1721 if (!leftComponentType.isInteger() || !rightComponentType.isInteger()) {
Brian Osmanbf2163f2020-09-16 16:21:40 -04001722 return false;
1723 }
1724 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001725 if (rightToLeftCost.isPossible(allowNarrowing) && rightToLeftCost < leftToRightCost) {
1726 // Right-to-Left conversion is possible and cheaper
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001727 *outLeftType = &left;
1728 *outRightType = &left;
1729 *outResultType = &left;
Brian Osman0acb5b52020-09-02 13:45:47 -04001730 } else if (leftToRightCost.isPossible(allowNarrowing)) {
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001731 // Left-to-Right conversion is possible (and at least as cheap as Right-to-Left)
1732 *outLeftType = &right;
1733 *outRightType = &right;
1734 *outResultType = &right;
1735 } else {
1736 return false;
1737 }
John Stiles56b1b802020-11-25 10:21:55 -05001738 if (op_is_logical(op)) {
Brian Osmanc4cb3a62020-09-03 16:52:28 -04001739 *outResultType = context.fBool_Type.get();
1740 }
1741 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001742 }
1743 return false;
1744}
1745
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001746static std::unique_ptr<Expression> short_circuit_boolean(const Context& context,
1747 const Expression& left,
1748 Token::Kind op,
1749 const Expression& right) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001750 SkASSERT(left.kind() == Expression::Kind::kBoolLiteral);
Ethan Nicholas59d660c2020-09-28 09:18:15 -04001751 bool leftVal = left.as<BoolLiteral>().value();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001752 if (op == Token::Kind::TK_LOGICALAND) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001753 // (true && expr) -> (expr) and (false && expr) -> (false)
1754 return leftVal ? right.clone()
1755 : std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, false));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001756 } else if (op == Token::Kind::TK_LOGICALOR) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001757 // (true || expr) -> (true) and (false || expr) -> (expr)
1758 return leftVal ? std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, true))
1759 : right.clone();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001760 } else if (op == Token::Kind::TK_LOGICALXOR) {
Noah Lavine334d0ba2019-12-18 23:03:49 -05001761 // (true ^^ expr) -> !(expr) and (false ^^ expr) -> (expr)
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001762 return leftVal ? std::unique_ptr<Expression>(new PrefixExpression(
1763 Token::Kind::TK_LOGICALNOT,
1764 right.clone()))
Noah Lavine334d0ba2019-12-18 23:03:49 -05001765 : right.clone();
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001766 } else {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001767 return nullptr;
1768 }
1769}
1770
John Stilescf27b4f2020-11-06 11:37:05 -05001771template <typename T>
1772std::unique_ptr<Expression> IRGenerator::constantFoldVector(const Expression& left,
1773 Token::Kind op,
1774 const Expression& right) const {
1775 SkASSERT(left.type() == right.type());
1776 const Type& type = left.type();
1777
1778 // Handle boolean operations: == !=
1779 if (op == Token::Kind::TK_EQEQ || op == Token::Kind::TK_NEQ) {
1780 if (left.kind() == right.kind()) {
1781 bool result = left.compareConstant(fContext, right) ^ (op == Token::Kind::TK_NEQ);
1782 return std::make_unique<BoolLiteral>(fContext, left.fOffset, result);
1783 }
1784 return nullptr;
1785 }
1786
1787 // Handle floating-point arithmetic: + - * /
1788 const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Constructor> {
1789 ExpressionArray args;
1790 for (int i = 0; i < type.columns(); i++) {
1791 T value = foldFn(left.getVecComponent<T>(i), right.getVecComponent<T>(i));
1792 args.push_back(std::make_unique<Literal<T>>(fContext, left.fOffset, value));
1793 }
1794 return std::make_unique<Constructor>(left.fOffset, &type, std::move(args));
1795 };
1796
1797 const auto isVectorDivisionByZero = [&]() -> bool {
1798 for (int i = 0; i < type.columns(); i++) {
1799 if (right.getVecComponent<T>(i) == 0) {
1800 return true;
1801 }
1802 }
1803 return false;
1804 };
1805
1806 switch (op) {
1807 case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](T a, T b) { return a + b; });
1808 case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](T a, T b) { return a - b; });
1809 case Token::Kind::TK_STAR: return vectorComponentwiseFold([](T a, T b) { return a * b; });
1810 case Token::Kind::TK_SLASH: {
1811 if (isVectorDivisionByZero()) {
1812 fErrors.error(right.fOffset, "division by zero");
1813 return nullptr;
1814 }
1815 return vectorComponentwiseFold([](T a, T b) { return a / b; });
1816 }
1817 default:
1818 return nullptr;
1819 }
1820}
1821
ethannicholas08a92112016-11-09 13:26:45 -08001822std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
1823 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001824 const Expression& right) const {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001825 // If the left side is a constant boolean literal, the right side does not need to be constant
1826 // for short circuit optimizations to allow the constant to be folded.
John Stiles95acbbc2020-11-04 16:23:26 -05001827 if (left.is<BoolLiteral>() && !right.isCompileTimeConstant()) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001828 return short_circuit_boolean(fContext, left, op, right);
John Stiles95acbbc2020-11-04 16:23:26 -05001829 } else if (right.is<BoolLiteral>() && !left.isCompileTimeConstant()) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001830 // There aren't side effects in SKSL within expressions, so (left OP right) is equivalent to
1831 // (right OP left) for short-circuit optimizations
1832 return short_circuit_boolean(fContext, right, op, left);
1833 }
1834
1835 // Other than the short-circuit cases above, constant folding requires both sides to be constant
Brian Osmanb6b95732020-06-30 11:44:27 -04001836 if (!left.isCompileTimeConstant() || !right.isCompileTimeConstant()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001837 return nullptr;
1838 }
ethannicholas08a92112016-11-09 13:26:45 -08001839 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
1840 // precision to calculate the results and hope the result makes sense. The plan is to move the
1841 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
1842 // types, which will let us be more intelligent about this.
John Stiles95acbbc2020-11-04 16:23:26 -05001843 if (left.is<BoolLiteral>() && right.is<BoolLiteral>()) {
Ethan Nicholas59d660c2020-09-28 09:18:15 -04001844 bool leftVal = left.as<BoolLiteral>().value();
1845 bool rightVal = right.as<BoolLiteral>().value();
ethannicholas08a92112016-11-09 13:26:45 -08001846 bool result;
1847 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001848 case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
1849 case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break;
1850 case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break;
ethannicholas08a92112016-11-09 13:26:45 -08001851 default: return nullptr;
1852 }
John Stiles95acbbc2020-11-04 16:23:26 -05001853 return std::make_unique<BoolLiteral>(fContext, left.fOffset, result);
ethannicholas08a92112016-11-09 13:26:45 -08001854 }
John Stilesfbd050b2020-08-03 13:21:46 -04001855 #define RESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \
1856 leftVal op rightVal)
1857 #define URESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \
1858 (uint32_t) leftVal op \
1859 (uint32_t) rightVal)
John Stiles95acbbc2020-11-04 16:23:26 -05001860 if (left.is<IntLiteral>() && right.is<IntLiteral>()) {
Ethan Nicholase96cdd12020-09-28 16:27:18 -04001861 int64_t leftVal = left.as<IntLiteral>().value();
1862 int64_t rightVal = right.as<IntLiteral>().value();
ethannicholas08a92112016-11-09 13:26:45 -08001863 switch (op) {
Ethan Nicholas66869e92020-04-30 09:27:54 -04001864 case Token::Kind::TK_PLUS: return URESULT(Int, +);
1865 case Token::Kind::TK_MINUS: return URESULT(Int, -);
1866 case Token::Kind::TK_STAR: return URESULT(Int, *);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001867 case Token::Kind::TK_SLASH:
Ethan Nicholas66869e92020-04-30 09:27:54 -04001868 if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) {
1869 fErrors.error(right.fOffset, "arithmetic overflow");
1870 return nullptr;
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001871 }
Ethan Nicholas66869e92020-04-30 09:27:54 -04001872 if (!rightVal) {
1873 fErrors.error(right.fOffset, "division by zero");
1874 return nullptr;
1875 }
1876 return RESULT(Int, /);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001877 case Token::Kind::TK_PERCENT:
Ethan Nicholas66869e92020-04-30 09:27:54 -04001878 if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) {
1879 fErrors.error(right.fOffset, "arithmetic overflow");
1880 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001881 }
Ethan Nicholas66869e92020-04-30 09:27:54 -04001882 if (!rightVal) {
1883 fErrors.error(right.fOffset, "division by zero");
1884 return nullptr;
1885 }
1886 return RESULT(Int, %);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001887 case Token::Kind::TK_BITWISEAND: return RESULT(Int, &);
1888 case Token::Kind::TK_BITWISEOR: return RESULT(Int, |);
1889 case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^);
1890 case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
1891 case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
1892 case Token::Kind::TK_GT: return RESULT(Bool, >);
1893 case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
1894 case Token::Kind::TK_LT: return RESULT(Bool, <);
1895 case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
1896 case Token::Kind::TK_SHL:
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001897 if (rightVal >= 0 && rightVal <= 31) {
Ethan Nicholase4489002020-04-29 14:00:14 -04001898 return URESULT(Int, <<);
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001899 }
1900 fErrors.error(right.fOffset, "shift value out of range");
1901 return nullptr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001902 case Token::Kind::TK_SHR:
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001903 if (rightVal >= 0 && rightVal <= 31) {
Ethan Nicholase4489002020-04-29 14:00:14 -04001904 return URESULT(Int, >>);
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001905 }
1906 fErrors.error(right.fOffset, "shift value out of range");
1907 return nullptr;
1908
1909 default:
1910 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001911 }
1912 }
John Stiles95acbbc2020-11-04 16:23:26 -05001913 if (left.is<FloatLiteral>() && right.is<FloatLiteral>()) {
Ethan Nicholasa3f22f12020-10-01 12:13:17 -04001914 SKSL_FLOAT leftVal = left.as<FloatLiteral>().value();
1915 SKSL_FLOAT rightVal = right.as<FloatLiteral>().value();
ethannicholas08a92112016-11-09 13:26:45 -08001916 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001917 case Token::Kind::TK_PLUS: return RESULT(Float, +);
1918 case Token::Kind::TK_MINUS: return RESULT(Float, -);
1919 case Token::Kind::TK_STAR: return RESULT(Float, *);
1920 case Token::Kind::TK_SLASH:
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001921 if (rightVal) {
1922 return RESULT(Float, /);
1923 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001924 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001925 return nullptr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001926 case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
1927 case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
1928 case Token::Kind::TK_GT: return RESULT(Bool, >);
1929 case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
1930 case Token::Kind::TK_LT: return RESULT(Bool, <);
1931 case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
1932 default: return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001933 }
1934 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001935 const Type& leftType = left.type();
1936 const Type& rightType = right.type();
John Stiles9aeed132020-11-24 17:36:06 -05001937 if (leftType.isVector() && leftType == rightType) {
John Stilescf27b4f2020-11-06 11:37:05 -05001938 if (leftType.componentType().isFloat()) {
1939 return constantFoldVector<SKSL_FLOAT>(left, op, right);
1940 } else if (leftType.componentType().isInteger()) {
1941 return constantFoldVector<SKSL_INT>(left, op, right);
Ethan Nicholascb670962017-04-20 19:31:52 -04001942 }
1943 }
John Stiles9aeed132020-11-24 17:36:06 -05001944 if (leftType.isMatrix() && rightType.isMatrix() && left.kind() == right.kind()) {
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001945 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001946 case Token::Kind::TK_EQEQ:
John Stiles95acbbc2020-11-04 16:23:26 -05001947 return std::make_unique<BoolLiteral>(fContext, left.fOffset,
John Stiles8e3b6be2020-10-13 11:14:08 -04001948 left.compareConstant(fContext, right));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001949 case Token::Kind::TK_NEQ:
John Stiles95acbbc2020-11-04 16:23:26 -05001950 return std::make_unique<BoolLiteral>(fContext, left.fOffset,
John Stiles8e3b6be2020-10-13 11:14:08 -04001951 !left.compareConstant(fContext, right));
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001952 default:
1953 return nullptr;
1954 }
1955 }
ethannicholas08a92112016-11-09 13:26:45 -08001956 #undef RESULT
1957 return nullptr;
1958}
1959
Ethan Nicholasfc994162019-06-06 10:04:27 -04001960std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1961 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1962 auto iter = expression.begin();
1963 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001964 if (!left) {
1965 return nullptr;
1966 }
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001967 Token::Kind op = expression.getToken().fKind;
John Stiles0f464502020-11-20 12:52:22 -05001968 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001969 if (!right) {
1970 return nullptr;
1971 }
ethannicholasd598f792016-07-25 10:08:54 -07001972 const Type* leftType;
1973 const Type* rightType;
1974 const Type* resultType;
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001975 const Type* rawLeftType;
John Stilesd0e48402020-09-22 14:00:40 -04001976 if (left->is<IntLiteral>() && right->type().isInteger()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001977 rawLeftType = &right->type();
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001978 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001979 rawLeftType = &left->type();
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001980 }
1981 const Type* rawRightType;
John Stilesd0e48402020-09-22 14:00:40 -04001982 if (right->is<IntLiteral>() && left->type().isInteger()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001983 rawRightType = &left->type();
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001984 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001985 rawRightType = &right->type();
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001986 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001987 if (!determine_binary_type(fContext, fSettings->fAllowNarrowingConversions, op,
1988 *rawLeftType, *rawRightType, &leftType, &rightType, &resultType)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001989 fErrors.error(expression.fOffset, String("type mismatch: '") +
Ethan Nicholasfc994162019-06-06 10:04:27 -04001990 Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04001991 "' cannot operate on '" + left->type().displayName() +
1992 "', '" + right->type().displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001993 return nullptr;
1994 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001995 if (Compiler::IsAssignment(op)) {
Ethan Nicholas4fadce42020-07-30 13:29:30 -04001996 if (!this->setRefKind(*left, op != Token::Kind::TK_EQ
Ethan Nicholas453f67f2020-10-09 10:43:45 -04001997 ? VariableReference::RefKind::kReadWrite
1998 : VariableReference::RefKind::kWrite)) {
Ethan Nicholas4fadce42020-07-30 13:29:30 -04001999 return nullptr;
2000 }
ethannicholasea4567c2016-10-17 11:24:37 -07002001 }
2002 left = this->coerce(std::move(left), *leftType);
2003 right = this->coerce(std::move(right), *rightType);
2004 if (!left || !right) {
2005 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07002006 }
John Stilesa008b0f2020-08-16 08:48:02 -04002007 std::unique_ptr<Expression> result = this->constantFold(*left, op, *right);
ethannicholas08a92112016-11-09 13:26:45 -08002008 if (!result) {
John Stilesd1c4dac2020-08-11 18:50:50 -04002009 result = std::make_unique<BinaryExpression>(expression.fOffset, std::move(left), op,
Ethan Nicholas30d30222020-09-11 12:27:26 -04002010 std::move(right), resultType);
ethannicholas08a92112016-11-09 13:26:45 -08002011 }
2012 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07002013}
2014
Ethan Nicholasfc994162019-06-06 10:04:27 -04002015std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
2016 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
2017 auto iter = node.begin();
2018 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
ethannicholasd598f792016-07-25 10:08:54 -07002019 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002020 if (!test) {
2021 return nullptr;
2022 }
John Stiles0f464502020-11-20 12:52:22 -05002023 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
2024 if (!ifTrue) {
2025 return nullptr;
2026 }
2027 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
2028 if (!ifFalse) {
2029 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07002030 }
ethannicholasd598f792016-07-25 10:08:54 -07002031 const Type* trueType;
2032 const Type* falseType;
2033 const Type* resultType;
Brian Osman0acb5b52020-09-02 13:45:47 -04002034 if (!determine_binary_type(fContext, fSettings->fAllowNarrowingConversions,
2035 Token::Kind::TK_EQEQ, ifTrue->type(), ifFalse->type(),
2036 &trueType, &falseType, &resultType) ||
2037 trueType != falseType) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002038 fErrors.error(node.fOffset, "ternary operator result mismatch: '" +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002039 ifTrue->type().displayName() + "', '" +
2040 ifFalse->type().displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002041 return nullptr;
2042 }
Brian Osman82329002020-07-21 09:39:27 -04002043 if (trueType->nonnullable() == *fContext.fFragmentProcessor_Type) {
2044 fErrors.error(node.fOffset,
2045 "ternary expression of type '" + trueType->displayName() + "' not allowed");
2046 return nullptr;
2047 }
ethannicholasd598f792016-07-25 10:08:54 -07002048 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05002049 if (!ifTrue) {
2050 return nullptr;
2051 }
ethannicholasd598f792016-07-25 10:08:54 -07002052 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05002053 if (!ifFalse) {
2054 return nullptr;
2055 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002056 if (test->kind() == Expression::Kind::kBoolLiteral) {
ethannicholas08a92112016-11-09 13:26:45 -08002057 // static boolean test, just return one of the branches
Ethan Nicholas59d660c2020-09-28 09:18:15 -04002058 if (test->as<BoolLiteral>().value()) {
ethannicholas08a92112016-11-09 13:26:45 -08002059 return ifTrue;
2060 } else {
2061 return ifFalse;
2062 }
2063 }
John Stiles8fa3b4e2020-09-02 11:27:23 -04002064 return std::make_unique<TernaryExpression>(node.fOffset,
2065 std::move(test),
2066 std::move(ifTrue),
2067 std::move(ifFalse));
ethannicholasb3058bd2016-07-01 08:22:01 -07002068}
2069
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002070void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04002071 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -04002072 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -04002073
2074 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
2075 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04002076 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
2077 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -04002078 std::sort(intrinsics.begin(), intrinsics.end(),
2079 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -04002080 if (a->isBuiltin() != b->isBuiltin()) {
2081 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -04002082 }
2083 if (a->fOffset != b->fOffset) {
2084 return a->fOffset < b->fOffset;
2085 }
Ethan Nicholase2c49992020-10-05 11:49:11 -04002086 if (a->name() != b->name()) {
2087 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -04002088 }
2089 return a->description() < b->description();
2090 });
2091 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002092 this->copyIntrinsicIfNeeded(*f);
2093 }
John Stiles607d36b2020-10-19 15:00:01 -04002094
Brian Osman0006ad02020-11-18 15:38:39 -05002095 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002096 }
2097}
2098
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002099std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05002100 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04002101 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04002102 if (function.isBuiltin()) {
2103 if (function.definition()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002104 fReferencedIntrinsics.insert(&function);
2105 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04002106 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002107 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05002108 }
2109 }
Ethan Nicholased84b732020-10-08 11:45:44 -04002110 if (function.parameters().size() != arguments.size()) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04002111 String msg = "call to '" + function.name() + "' expected " +
Ethan Nicholased84b732020-10-08 11:45:44 -04002112 to_string((uint64_t) function.parameters().size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07002113 " argument";
Ethan Nicholased84b732020-10-08 11:45:44 -04002114 if (function.parameters().size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002115 msg += "s";
2116 }
ethannicholas5961bc92016-10-12 06:39:56 -07002117 msg += ", but found " + to_string((uint64_t) arguments.size());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002118 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07002119 return nullptr;
2120 }
Ethan Nicholased84b732020-10-08 11:45:44 -04002121 if (fKind == Program::kPipelineStage_Kind && !function.definition() && !function.isBuiltin()) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04002122 String msg = "call to undefined function '" + function.name() + "'";
Brian Osman5f6b41e2020-03-09 11:53:24 -04002123 fErrors.error(offset, msg);
2124 return nullptr;
2125 }
John Stilesfa889112020-10-12 19:03:43 -04002126 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07002127 const Type* returnType;
2128 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04002129 String msg = "no match for " + function.name() + "(";
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002130 String separator;
ethannicholas471e8942016-10-28 09:02:46 -07002131 for (size_t i = 0; i < arguments.size(); i++) {
2132 msg += separator;
2133 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04002134 msg += arguments[i]->type().displayName();
ethannicholas471e8942016-10-28 09:02:46 -07002135 }
2136 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002137 fErrors.error(offset, msg);
ethannicholas471e8942016-10-28 09:02:46 -07002138 return nullptr;
2139 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002140 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07002141 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07002142 if (!arguments[i]) {
2143 return nullptr;
2144 }
Ethan Nicholased84b732020-10-08 11:45:44 -04002145 const Modifiers& paramModifiers = function.parameters()[i]->modifiers();
John Stiles978674a2020-09-23 15:24:51 -04002146 if (paramModifiers.fFlags & Modifiers::kOut_Flag) {
2147 if (!this->setRefKind(*arguments[i], paramModifiers.fFlags & Modifiers::kIn_Flag
Ethan Nicholas453f67f2020-10-09 10:43:45 -04002148 ? VariableReference::RefKind::kReadWrite
2149 : VariableReference::RefKind::kPointer)) {
John Stiles978674a2020-09-23 15:24:51 -04002150 return nullptr;
2151 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002152 }
2153 }
John Stilesea9ab822020-08-31 09:55:04 -04002154
John Stiles0f464502020-11-20 12:52:22 -05002155 return std::make_unique<FunctionCall>(offset, returnType, &function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07002156}
2157
2158/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04002159 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04002160 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
2161 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07002162 */
Brian Osman0acb5b52020-09-02 13:45:47 -04002163CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04002164 const ExpressionArray& arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04002165 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04002166 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07002167 }
John Stilesfa889112020-10-12 19:03:43 -04002168 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07002169 const Type* ignored;
2170 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04002171 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07002172 }
Brian Osman0acb5b52020-09-02 13:45:47 -04002173 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07002174 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04002175 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07002176 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04002177 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07002178}
2179
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002180std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05002181 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04002182 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002183 switch (functionValue->kind()) {
2184 case Expression::Kind::kTypeReference:
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002185 return this->convertConstructor(offset,
Ethan Nicholas5194a702020-10-12 11:12:12 -04002186 functionValue->as<TypeReference>().value(),
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002187 std::move(arguments));
Ethan Nicholase6592142020-09-08 10:22:09 -04002188 case Expression::Kind::kExternalValue: {
Ethan Nicholas444ccc62020-10-09 10:16:22 -04002189 const ExternalValue& v = functionValue->as<ExternalValueReference>().value();
2190 if (!v.canCall()) {
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002191 fErrors.error(offset, "this external value is not a function");
2192 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07002193 }
Ethan Nicholas444ccc62020-10-09 10:16:22 -04002194 int count = v.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002195 if (count != (int) arguments.size()) {
2196 fErrors.error(offset, "external function expected " + to_string(count) +
2197 " arguments, but found " + to_string((int) arguments.size()));
2198 return nullptr;
2199 }
2200 static constexpr int PARAMETER_MAX = 16;
2201 SkASSERT(count < PARAMETER_MAX);
2202 const Type* types[PARAMETER_MAX];
Ethan Nicholas444ccc62020-10-09 10:16:22 -04002203 v.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002204 for (int i = 0; i < count; ++i) {
2205 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
2206 if (!arguments[i]) {
2207 return nullptr;
2208 }
2209 }
Ethan Nicholas444ccc62020-10-09 10:16:22 -04002210 return std::make_unique<ExternalFunctionCall>(offset, &v, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07002211 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002212 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04002213 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04002214 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman0acb5b52020-09-02 13:45:47 -04002215 CoercionCost bestCost = CoercionCost::Impossible();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002216 const FunctionDeclaration* best = nullptr;
Ethan Nicholas5194a702020-10-12 11:12:12 -04002217 if (functions.size() > 1) {
2218 for (const auto& f : functions) {
Brian Osman0acb5b52020-09-02 13:45:47 -04002219 CoercionCost cost = this->callCost(*f, arguments);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002220 if (cost < bestCost) {
2221 bestCost = cost;
2222 best = f;
2223 }
2224 }
2225 if (best) {
2226 return this->call(offset, *best, std::move(arguments));
2227 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04002228 String msg = "no match for " + functions[0]->name() + "(";
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002229 String separator;
2230 for (size_t i = 0; i < arguments.size(); i++) {
2231 msg += separator;
2232 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -04002233 msg += arguments[i]->type().displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002234 }
2235 msg += ")";
2236 fErrors.error(offset, msg);
2237 return nullptr;
2238 }
Ethan Nicholas5194a702020-10-12 11:12:12 -04002239 return this->call(offset, *functions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07002240 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002241 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002242 fErrors.error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04002243 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07002244 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002245}
2246
John Stiles8e3b6be2020-10-13 11:14:08 -04002247std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(int offset,
2248 const Type& type,
2249 ExpressionArray args) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002250 SkASSERT(type.isNumber());
Ethan Nicholas84645e32017-02-09 13:57:14 -05002251 if (args.size() != 1) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002252 fErrors.error(offset, "invalid arguments to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002253 "' constructor, (expected exactly 1 argument, but found " +
2254 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -07002255 return nullptr;
2256 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002257 const Type& argType = args[0]->type();
2258 if (type == argType) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04002259 return std::move(args[0]);
2260 }
John Stilesd0e48402020-09-22 14:00:40 -04002261 if (type.isFloat() && args.size() == 1 && args[0]->is<FloatLiteral>()) {
Ethan Nicholasa3f22f12020-10-01 12:13:17 -04002262 SKSL_FLOAT value = args[0]->as<FloatLiteral>().value();
John Stilesd0e48402020-09-22 14:00:40 -04002263 return std::make_unique<FloatLiteral>(offset, value, &type);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04002264 }
John Stilesd0e48402020-09-22 14:00:40 -04002265 if (type.isFloat() && args.size() == 1 && args[0]->is<IntLiteral>()) {
Ethan Nicholase96cdd12020-09-28 16:27:18 -04002266 int64_t value = args[0]->as<IntLiteral>().value();
Ethan Nicholasa3f22f12020-10-01 12:13:17 -04002267 return std::make_unique<FloatLiteral>(offset, (float)value, &type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002268 }
John Stilesd0e48402020-09-22 14:00:40 -04002269 if (args[0]->is<IntLiteral>() && (type == *fContext.fInt_Type ||
2270 type == *fContext.fUInt_Type)) {
Ethan Nicholase96cdd12020-09-28 16:27:18 -04002271 return std::make_unique<IntLiteral>(offset, args[0]->as<IntLiteral>().value(), &type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002272 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002273 if (argType == *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002274 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0));
2275 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1));
John Stilesd0e48402020-09-22 14:00:40 -04002276 return std::make_unique<TernaryExpression>(offset, std::move(args[0]),
2277 this->coerce(std::move(one), type),
2278 this->coerce(std::move(zero), type));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002279 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002280 if (!argType.isNumber()) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002281 fErrors.error(offset, "invalid argument to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002282 "' constructor (expected a number or bool, but found '" +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002283 argType.displayName() + "')");
Ethan Nicholas84645e32017-02-09 13:57:14 -05002284 return nullptr;
2285 }
John Stilesd0e48402020-09-22 14:00:40 -04002286 return std::make_unique<Constructor>(offset, &type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002287}
2288
John Stiles36374402020-08-13 12:16:44 -04002289static int component_count(const Type& type) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002290 switch (type.typeKind()) {
2291 case Type::TypeKind::kVector:
Ethan Nicholas84645e32017-02-09 13:57:14 -05002292 return type.columns();
Ethan Nicholase6592142020-09-08 10:22:09 -04002293 case Type::TypeKind::kMatrix:
Ethan Nicholas84645e32017-02-09 13:57:14 -05002294 return type.columns() * type.rows();
2295 default:
2296 return 1;
2297 }
2298}
2299
John Stiles8e3b6be2020-10-13 11:14:08 -04002300std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(int offset,
2301 const Type& type,
2302 ExpressionArray args) {
John Stiles9aeed132020-11-24 17:36:06 -05002303 SkASSERT(type.isVector() || type.isMatrix());
2304 if (type.isMatrix() && args.size() == 1 && args[0]->type().isMatrix()) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05002305 // matrix from matrix is always legal
Ethan Nicholas30d30222020-09-11 12:27:26 -04002306 return std::unique_ptr<Expression>(new Constructor(offset, &type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002307 }
2308 int actual = 0;
2309 int expected = type.rows() * type.columns();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002310 if (args.size() != 1 || expected != component_count(args[0]->type()) ||
2311 type.componentType().isNumber() != args[0]->type().componentType().isNumber()) {
ethannicholas5961bc92016-10-12 06:39:56 -07002312 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002313 const Type& argType = args[i]->type();
John Stiles9aeed132020-11-24 17:36:06 -05002314 if (argType.isVector()) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002315 if (type.componentType().isNumber() !=
Ethan Nicholas30d30222020-09-11 12:27:26 -04002316 argType.componentType().isNumber()) {
2317 fErrors.error(offset, "'" + argType.displayName() + "' is not a valid "
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002318 "parameter to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002319 "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002320 return nullptr;
2321 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002322 actual += argType.columns();
John Stiles9aeed132020-11-24 17:36:06 -05002323 } else if (argType.isScalar()) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002324 actual += 1;
John Stiles9aeed132020-11-24 17:36:06 -05002325 if (!type.isScalar()) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002326 args[i] = this->coerce(std::move(args[i]), type.componentType());
2327 if (!args[i]) {
2328 return nullptr;
2329 }
2330 }
2331 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002332 fErrors.error(offset, "'" + argType.displayName() + "' is not a valid "
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002333 "parameter to '" + type.displayName() + "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002334 return nullptr;
2335 }
2336 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05002337 if (actual != 1 && actual != expected) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002338 fErrors.error(offset, "invalid arguments to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002339 "' constructor (expected " + to_string(expected) +
2340 " scalars, but found " + to_string(actual) + ")");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002341 return nullptr;
2342 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002343 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002344 return std::unique_ptr<Expression>(new Constructor(offset, &type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002345}
2346
John Stiles8e3b6be2020-10-13 11:14:08 -04002347std::unique_ptr<Expression> IRGenerator::convertConstructor(int offset,
2348 const Type& type,
2349 ExpressionArray args) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05002350 // FIXME: add support for structs
Ethan Nicholas30d30222020-09-11 12:27:26 -04002351 if (args.size() == 1 && args[0]->type() == type &&
Brian Osman82329002020-07-21 09:39:27 -04002352 type.nonnullable() != *fContext.fFragmentProcessor_Type) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05002353 // argument is already the right type, just return it
2354 return std::move(args[0]);
2355 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002356 Type::TypeKind kind = type.typeKind();
Ethan Nicholas84645e32017-02-09 13:57:14 -05002357 if (type.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002358 return this->convertNumberConstructor(offset, type, std::move(args));
Ethan Nicholase6592142020-09-08 10:22:09 -04002359 } else if (kind == Type::TypeKind::kArray) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05002360 const Type& base = type.componentType();
2361 for (size_t i = 0; i < args.size(); i++) {
2362 args[i] = this->coerce(std::move(args[i]), base);
2363 if (!args[i]) {
2364 return nullptr;
2365 }
2366 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002367 return std::make_unique<Constructor>(offset, &type, std::move(args));
Ethan Nicholase6592142020-09-08 10:22:09 -04002368 } else if (kind == Type::TypeKind::kVector || kind == Type::TypeKind::kMatrix) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002369 return this->convertCompoundConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002370 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002371 fErrors.error(offset, "cannot construct '" + type.displayName() + "'");
Ethan Nicholas84645e32017-02-09 13:57:14 -05002372 return nullptr;
2373 }
2374}
2375
Ethan Nicholasfc994162019-06-06 10:04:27 -04002376std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
2377 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
2378 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07002379 if (!base) {
2380 return nullptr;
2381 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002382 const Type& baseType = base->type();
Ethan Nicholasfc994162019-06-06 10:04:27 -04002383 switch (expression.getToken().fKind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002384 case Token::Kind::TK_PLUS:
John Stiles9aeed132020-11-24 17:36:06 -05002385 if (!baseType.isNumber() && !baseType.isVector() &&
Ethan Nicholas30d30222020-09-11 12:27:26 -04002386 baseType != *fContext.fFloatLiteral_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002387 fErrors.error(expression.fOffset,
Ethan Nicholas30d30222020-09-11 12:27:26 -04002388 "'+' cannot operate on '" + baseType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002389 return nullptr;
2390 }
2391 return base;
John Stiles978674a2020-09-23 15:24:51 -04002392
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002393 case Token::Kind::TK_MINUS:
John Stiles978674a2020-09-23 15:24:51 -04002394 if (base->is<IntLiteral>()) {
2395 return std::make_unique<IntLiteral>(fContext, base->fOffset,
Ethan Nicholase96cdd12020-09-28 16:27:18 -04002396 -base->as<IntLiteral>().value());
ethannicholasb3058bd2016-07-01 08:22:01 -07002397 }
John Stiles978674a2020-09-23 15:24:51 -04002398 if (base->is<FloatLiteral>()) {
2399 return std::make_unique<FloatLiteral>(fContext, base->fOffset,
Ethan Nicholasa3f22f12020-10-01 12:13:17 -04002400 -base->as<FloatLiteral>().value());
ethannicholasb3058bd2016-07-01 08:22:01 -07002401 }
John Stiles318da832020-11-25 11:09:03 -05002402 if (!baseType.isNumber() &&
2403 !(baseType.isVector() && baseType.componentType().isNumber())) {
Ethan Nicholase1f55022019-02-05 17:17:40 -05002404 fErrors.error(expression.fOffset,
Ethan Nicholas30d30222020-09-11 12:27:26 -04002405 "'-' cannot operate on '" + baseType.displayName() + "'");
Ethan Nicholase1f55022019-02-05 17:17:40 -05002406 return nullptr;
2407 }
John Stiles978674a2020-09-23 15:24:51 -04002408 return std::make_unique<PrefixExpression>(Token::Kind::TK_MINUS, std::move(base));
2409
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002410 case Token::Kind::TK_PLUSPLUS:
Ethan Nicholas30d30222020-09-11 12:27:26 -04002411 if (!baseType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002412 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002413 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002414 "' cannot operate on '" + baseType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002415 return nullptr;
2416 }
Ethan Nicholas453f67f2020-10-09 10:43:45 -04002417 if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) {
John Stiles978674a2020-09-23 15:24:51 -04002418 return nullptr;
2419 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002420 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002421 case Token::Kind::TK_MINUSMINUS:
Ethan Nicholas30d30222020-09-11 12:27:26 -04002422 if (!baseType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002423 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002424 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002425 "' cannot operate on '" + baseType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002426 return nullptr;
2427 }
Ethan Nicholas453f67f2020-10-09 10:43:45 -04002428 if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) {
John Stiles978674a2020-09-23 15:24:51 -04002429 return nullptr;
2430 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002431 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002432 case Token::Kind::TK_LOGICALNOT:
John Stiles318da832020-11-25 11:09:03 -05002433 if (!baseType.isBoolean()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002434 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002435 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002436 "' cannot operate on '" + baseType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002437 return nullptr;
2438 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002439 if (base->kind() == Expression::Kind::kBoolLiteral) {
John Stiles978674a2020-09-23 15:24:51 -04002440 return std::make_unique<BoolLiteral>(fContext, base->fOffset,
Ethan Nicholas59d660c2020-09-28 09:18:15 -04002441 !base->as<BoolLiteral>().value());
ethannicholas08a92112016-11-09 13:26:45 -08002442 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002443 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002444 case Token::Kind::TK_BITWISENOT:
Ethan Nicholas30d30222020-09-11 12:27:26 -04002445 if (baseType != *fContext.fInt_Type && baseType != *fContext.fUInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002446 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002447 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002448 "' cannot operate on '" + baseType.displayName() + "'");
ethannicholas5961bc92016-10-12 06:39:56 -07002449 return nullptr;
2450 }
2451 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05002452 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07002453 ABORT("unsupported prefix operator\n");
2454 }
John Stiles978674a2020-09-23 15:24:51 -04002455 return std::make_unique<PrefixExpression>(expression.getToken().fKind, std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07002456}
2457
2458std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002459 const ASTNode& index) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002460 if (base->kind() == Expression::Kind::kTypeReference) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002461 if (index.fKind == ASTNode::Kind::kInt) {
Ethan Nicholas5194a702020-10-12 11:12:12 -04002462 const Type& oldType = base->as<TypeReference>().value();
John Stiles6bef6a72020-12-02 14:26:04 -05002463 SkSTArray<1, int> dimension = {index.getInt()};
2464 const Type* newType = fSymbolTable->addArrayDimensions(&oldType, dimension);
Ethan Nicholase6592142020-09-08 10:22:09 -04002465 return std::make_unique<TypeReference>(fContext, base->fOffset, newType);
Ethan Nicholas50afc172017-02-16 14:49:57 -05002466
2467 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002468 fErrors.error(base->fOffset, "array size must be a constant");
Ethan Nicholas50afc172017-02-16 14:49:57 -05002469 return nullptr;
2470 }
2471 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002472 const Type& baseType = base->type();
2473 if (baseType.typeKind() != Type::TypeKind::kArray &&
John Stiles9aeed132020-11-24 17:36:06 -05002474 !baseType.isMatrix() &&
2475 !baseType.isVector()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002476 fErrors.error(base->fOffset, "expected array, but found '" + baseType.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002477 "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002478 return nullptr;
2479 }
2480 std::unique_ptr<Expression> converted = this->convertExpression(index);
2481 if (!converted) {
2482 return nullptr;
2483 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002484 if (converted->type() != *fContext.fUInt_Type) {
ethannicholas5961bc92016-10-12 06:39:56 -07002485 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
2486 if (!converted) {
2487 return nullptr;
2488 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002489 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002490 return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(converted));
ethannicholasb3058bd2016-07-01 08:22:01 -07002491}
2492
2493std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002494 StringFragment field) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002495 if (base->kind() == Expression::Kind::kExternalValue) {
Ethan Nicholas444ccc62020-10-09 10:16:22 -04002496 const ExternalValue& ev = base->as<ExternalValueReference>().value();
Ethan Nicholas91164d12019-05-15 15:29:54 -04002497 ExternalValue* result = ev.getChild(String(field).c_str());
2498 if (!result) {
2499 fErrors.error(base->fOffset, "external value does not have a child named '" + field +
2500 "'");
2501 return nullptr;
2502 }
2503 return std::unique_ptr<Expression>(new ExternalValueReference(base->fOffset, result));
2504 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002505 const Type& baseType = base->type();
2506 auto fields = baseType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07002507 for (size_t i = 0; i < fields.size(); i++) {
2508 if (fields[i].fName == field) {
2509 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
2510 }
2511 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002512 fErrors.error(base->fOffset, "type '" + baseType.displayName() + "' does not have a field "
John Stiles68861e32020-09-25 16:02:07 -04002513 "named '" + field + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002514 return nullptr;
2515}
2516
Brian Osman25647672020-09-15 15:16:56 -04002517// Swizzles are complicated due to constant components. The most difficult case is a mask like
John Stiles6e49a372020-09-16 13:40:54 -04002518// '.x1w0'. A naive approach might turn that into 'float4(base.x, 1, base.w, 0)', but that evaluates
2519// 'base' twice. We instead group the swizzle mask ('xw') and constants ('1, 0') together and use a
Brian Osman25647672020-09-15 15:16:56 -04002520// secondary swizzle to put them back into the right order, so in this case we end up with
John Stiles6e49a372020-09-16 13:40:54 -04002521// 'float4(base.xw, 1, 0).xzyw'.
ethannicholasb3058bd2016-07-01 08:22:01 -07002522std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002523 StringFragment fields) {
Brian Osman25647672020-09-15 15:16:56 -04002524 const int offset = base->fOffset;
Ethan Nicholas30d30222020-09-11 12:27:26 -04002525 const Type& baseType = base->type();
John Stiles9aeed132020-11-24 17:36:06 -05002526 if (!baseType.isVector() && !baseType.isNumber()) {
Brian Osman25647672020-09-15 15:16:56 -04002527 fErrors.error(offset, "cannot swizzle value of type '" + baseType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002528 return nullptr;
2529 }
Brian Osman25647672020-09-15 15:16:56 -04002530
2531 if (fields.fLength > 4) {
2532 fErrors.error(offset, "too many components in swizzle mask '" + fields + "'");
2533 return nullptr;
2534 }
2535
John Stiles750109b2020-10-30 13:45:46 -04002536 ComponentArray maskComponents;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002537 for (size_t i = 0; i < fields.fLength; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05002538 switch (fields[i]) {
Ethan Nicholasac285b12019-02-12 16:05:18 -05002539 case '0':
Ethan Nicholasac285b12019-02-12 16:05:18 -05002540 case '1':
John Stiles6e49a372020-09-16 13:40:54 -04002541 // Skip over constant fields for now.
Ethan Nicholasac285b12019-02-12 16:05:18 -05002542 break;
Ethan Nicholase455f652019-09-13 12:52:55 -04002543 case 'x':
2544 case 'r':
Ethan Nicholas11d53972016-11-28 11:23:23 -05002545 case 's':
Ethan Nicholase455f652019-09-13 12:52:55 -04002546 case 'L':
Brian Osman25647672020-09-15 15:16:56 -04002547 maskComponents.push_back(0);
ethannicholasb3058bd2016-07-01 08:22:01 -07002548 break;
Ethan Nicholase455f652019-09-13 12:52:55 -04002549 case 'y':
2550 case 'g':
ethannicholasb3058bd2016-07-01 08:22:01 -07002551 case 't':
Ethan Nicholase455f652019-09-13 12:52:55 -04002552 case 'T':
Ethan Nicholas30d30222020-09-11 12:27:26 -04002553 if (baseType.columns() >= 2) {
Brian Osman25647672020-09-15 15:16:56 -04002554 maskComponents.push_back(1);
ethannicholasb3058bd2016-07-01 08:22:01 -07002555 break;
2556 }
John Stiles30212b72020-06-11 17:55:07 -04002557 [[fallthrough]];
Ethan Nicholase455f652019-09-13 12:52:55 -04002558 case 'z':
2559 case 'b':
Ethan Nicholas11d53972016-11-28 11:23:23 -05002560 case 'p':
Ethan Nicholase455f652019-09-13 12:52:55 -04002561 case 'R':
Ethan Nicholas30d30222020-09-11 12:27:26 -04002562 if (baseType.columns() >= 3) {
Brian Osman25647672020-09-15 15:16:56 -04002563 maskComponents.push_back(2);
ethannicholasb3058bd2016-07-01 08:22:01 -07002564 break;
2565 }
John Stiles30212b72020-06-11 17:55:07 -04002566 [[fallthrough]];
Ethan Nicholase455f652019-09-13 12:52:55 -04002567 case 'w':
2568 case 'a':
ethannicholasb3058bd2016-07-01 08:22:01 -07002569 case 'q':
Ethan Nicholase455f652019-09-13 12:52:55 -04002570 case 'B':
Ethan Nicholas30d30222020-09-11 12:27:26 -04002571 if (baseType.columns() >= 4) {
Brian Osman25647672020-09-15 15:16:56 -04002572 maskComponents.push_back(3);
ethannicholasb3058bd2016-07-01 08:22:01 -07002573 break;
2574 }
John Stiles30212b72020-06-11 17:55:07 -04002575 [[fallthrough]];
ethannicholasb3058bd2016-07-01 08:22:01 -07002576 default:
Brian Osman25647672020-09-15 15:16:56 -04002577 fErrors.error(offset, String::printf("invalid swizzle component '%c'", fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07002578 return nullptr;
2579 }
2580 }
Brian Osman25647672020-09-15 15:16:56 -04002581 if (maskComponents.empty()) {
2582 fErrors.error(offset, "swizzle must refer to base expression");
ethannicholasb3058bd2016-07-01 08:22:01 -07002583 return nullptr;
2584 }
Brian Osman25647672020-09-15 15:16:56 -04002585
2586 // First, we need a vector expression that is the non-constant portion of the swizzle, packed:
2587 // scalar.xxx -> type3(scalar)
2588 // scalar.x0x0 -> type2(scalar)
2589 // vector.zyx -> vector.zyx
2590 // vector.x0y0 -> vector.xy
2591 std::unique_ptr<Expression> expr;
Ethan Nicholas30d30222020-09-11 12:27:26 -04002592 if (baseType.isNumber()) {
John Stiles8e3b6be2020-10-13 11:14:08 -04002593 ExpressionArray scalarConstructorArgs;
Brian Osman25647672020-09-15 15:16:56 -04002594 scalarConstructorArgs.push_back(std::move(base));
2595 expr = std::make_unique<Constructor>(
2596 offset, &baseType.toCompound(fContext, maskComponents.size(), 1),
2597 std::move(scalarConstructorArgs));
2598 } else {
2599 expr = std::make_unique<Swizzle>(fContext, std::move(base), maskComponents);
Ethan Nicholas7b9da252020-08-03 13:43:50 -04002600 }
Brian Osman25647672020-09-15 15:16:56 -04002601
John Stiles6e49a372020-09-16 13:40:54 -04002602 // If we have processed the entire swizzle, we're done.
2603 if (maskComponents.size() == fields.fLength) {
Brian Osman25647672020-09-15 15:16:56 -04002604 return expr;
2605 }
2606
2607 // Now we create a constructor that has the correct number of elements for the final swizzle,
John Stiles6e49a372020-09-16 13:40:54 -04002608 // with all fields at the start. It's not finished yet; constants we need will be added below.
2609 // scalar.x0x0 -> type4(type2(x), ...)
2610 // vector.y111 -> type4(vector.y, ...)
2611 // vector.z10x -> type4(vector.zx, ...)
Brian Osman25647672020-09-15 15:16:56 -04002612 //
John Stiles6e49a372020-09-16 13:40:54 -04002613 // We could create simpler IR in some cases by reordering here, if all fields are packed
Brian Osman25647672020-09-15 15:16:56 -04002614 // contiguously. The benefits are minor, so skip the optimization to keep the algorithm simple.
John Stiles6e49a372020-09-16 13:40:54 -04002615 // The constructor will have at most three arguments: { base value, constant 0, constant 1 }
John Stiles8e3b6be2020-10-13 11:14:08 -04002616 ExpressionArray constructorArgs;
John Stilesf4bda742020-10-14 16:57:41 -04002617 constructorArgs.reserve_back(3);
Brian Osman25647672020-09-15 15:16:56 -04002618 constructorArgs.push_back(std::move(expr));
John Stiles6e49a372020-09-16 13:40:54 -04002619
2620 // Apply another swizzle to shuffle the constants into the correct place. Any constant values we
2621 // need are also tacked on to the end of the constructor.
2622 // scalar.x0x0 -> type4(type2(x), 0).xyxy
2623 // vector.y111 -> type4(vector.y, 1).xyyy
2624 // vector.z10x -> type4(vector.zx, 1, 0).xzwy
Brian Osman25647672020-09-15 15:16:56 -04002625 const Type* numberType = baseType.isNumber() ? &baseType : &baseType.componentType();
John Stiles750109b2020-10-30 13:45:46 -04002626 ComponentArray swizzleComponents;
John Stiles6e49a372020-09-16 13:40:54 -04002627 int maskFieldIdx = 0;
2628 int constantFieldIdx = maskComponents.size();
2629 int constantZeroIdx = -1, constantOneIdx = -1;
Brian Osman25647672020-09-15 15:16:56 -04002630
Brian Osman25647672020-09-15 15:16:56 -04002631 for (size_t i = 0; i < fields.fLength; i++) {
John Stiles6e49a372020-09-16 13:40:54 -04002632 switch (fields[i]) {
2633 case '0':
2634 if (constantZeroIdx == -1) {
John Stilesd0e48402020-09-22 14:00:40 -04002635 // Synthesize a 'type(0)' argument at the end of the constructor.
John Stiles8e3b6be2020-10-13 11:14:08 -04002636 auto zero = std::make_unique<Constructor>(offset, numberType,
2637 ExpressionArray{});
Ethan Nicholasf70f0442020-09-29 12:41:35 -04002638 zero->arguments().push_back(std::make_unique<IntLiteral>(fContext, offset,
2639 /*fValue=*/0));
John Stilesd0e48402020-09-22 14:00:40 -04002640 constructorArgs.push_back(std::move(zero));
John Stiles6e49a372020-09-16 13:40:54 -04002641 constantZeroIdx = constantFieldIdx++;
2642 }
2643 swizzleComponents.push_back(constantZeroIdx);
2644 break;
2645 case '1':
2646 if (constantOneIdx == -1) {
John Stilesd0e48402020-09-22 14:00:40 -04002647 // Synthesize a 'type(1)' argument at the end of the constructor.
John Stiles8e3b6be2020-10-13 11:14:08 -04002648 auto one = std::make_unique<Constructor>(offset, numberType, ExpressionArray{});
Ethan Nicholasf70f0442020-09-29 12:41:35 -04002649 one->arguments().push_back(std::make_unique<IntLiteral>(fContext, offset,
2650 /*fValue=*/1));
John Stilesd0e48402020-09-22 14:00:40 -04002651 constructorArgs.push_back(std::move(one));
John Stiles6e49a372020-09-16 13:40:54 -04002652 constantOneIdx = constantFieldIdx++;
2653 }
2654 swizzleComponents.push_back(constantOneIdx);
2655 break;
2656 default:
2657 // The non-constant fields are already in the expected order.
2658 swizzleComponents.push_back(maskFieldIdx++);
2659 break;
Brian Osman25647672020-09-15 15:16:56 -04002660 }
2661 }
2662
John Stiles6e49a372020-09-16 13:40:54 -04002663 expr = std::make_unique<Constructor>(offset,
2664 &numberType->toCompound(fContext, constantFieldIdx, 1),
2665 std::move(constructorArgs));
2666
John Stilesb23ea382020-09-16 13:41:14 -04002667 // For some of our most common use cases ('.xyz0', '.xyz1'), we will now have an identity
2668 // swizzle; in those cases we can just return the constructor without the swizzle attached.
2669 for (size_t i = 0; i < swizzleComponents.size(); ++i) {
2670 if (swizzleComponents[i] != int(i)) {
2671 // The swizzle has an effect, so apply it.
John Stiles750109b2020-10-30 13:45:46 -04002672 return std::make_unique<Swizzle>(fContext, std::move(expr), swizzleComponents);
John Stilesb23ea382020-09-16 13:41:14 -04002673 }
2674 }
2675
2676 // The swizzle was a no-op; return the constructor expression directly.
2677 return expr;
ethannicholasb3058bd2016-07-01 08:22:01 -07002678}
2679
Ethan Nicholas01ec7e82020-10-08 12:10:12 -04002680const Type* IRGenerator::typeForSetting(int offset, String name) const {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002681 auto found = fCapsMap.find(name);
2682 if (found == fCapsMap.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002683 fErrors.error(offset, "unknown capability flag '" + name + "'");
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002684 return nullptr;
2685 }
Ethan Nicholas01ec7e82020-10-08 12:10:12 -04002686 switch (found->second.fKind) {
2687 case Program::Settings::Value::kBool_Kind: return fContext.fBool_Type.get();
2688 case Program::Settings::Value::kFloat_Kind: return fContext.fFloat_Type.get();
2689 case Program::Settings::Value::kInt_Kind: return fContext.fInt_Type.get();
2690 }
2691 SkUNREACHABLE;
2692 return nullptr;
2693}
2694
2695std::unique_ptr<Expression> IRGenerator::valueForSetting(int offset, String name) const {
2696 auto found = fCapsMap.find(name);
2697 if (found == fCapsMap.end()) {
2698 fErrors.error(offset, "unknown capability flag '" + name + "'");
2699 return nullptr;
2700 }
2701 return found->second.literal(fContext, offset);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002702}
2703
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002704std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type,
2705 StringFragment field) {
Brian Osman1313d1a2020-09-08 10:34:30 -04002706 // Find the Enum element that this type refers to (if any)
Brian Osman2b469eb2020-09-21 11:32:10 -04002707 const ProgramElement* enumElement = nullptr;
2708 for (const auto& e : *fProgramElements) {
Ethan Nicholasd83ded82020-09-29 17:05:54 -04002709 if (e->is<Enum>() && type.name() == e->as<Enum>().typeName()) {
Brian Osman2b469eb2020-09-21 11:32:10 -04002710 enumElement = e.get();
2711 break;
Brian Osman1313d1a2020-09-08 10:34:30 -04002712 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002713 }
Brian Osman1313d1a2020-09-08 10:34:30 -04002714
2715 if (enumElement) {
2716 // We found the Enum element. Look for 'field' as a member.
2717 std::shared_ptr<SymbolTable> old = fSymbolTable;
Ethan Nicholasd83ded82020-09-29 17:05:54 -04002718 fSymbolTable = enumElement->as<Enum>().symbols();
Brian Osman1313d1a2020-09-08 10:34:30 -04002719 std::unique_ptr<Expression> result = convertIdentifier(
2720 ASTNode(&fFile->fNodes, offset, ASTNode::Kind::kIdentifier, field));
2721 if (result) {
Ethan Nicholas78686922020-10-08 06:46:27 -04002722 const Variable& v = *result->as<VariableReference>().variable();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04002723 SkASSERT(v.initialValue());
Brian Osman1313d1a2020-09-08 10:34:30 -04002724 result = std::make_unique<IntLiteral>(
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04002725 offset, v.initialValue()->as<IntLiteral>().value(), &type);
Brian Osman1313d1a2020-09-08 10:34:30 -04002726 } else {
2727 fErrors.error(offset,
Ethan Nicholase2c49992020-10-05 11:49:11 -04002728 "type '" + type.name() + "' does not have a member named '" + field +
2729 "'");
Brian Osman1313d1a2020-09-08 10:34:30 -04002730 }
2731 fSymbolTable = old;
2732 return result;
2733 } else {
2734 // No Enum element? Check the intrinsics, clone it into the program, try again.
Brian Osman00a8b5b2020-10-02 09:06:04 -04002735 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04002736 if (const ProgramElement* found = fIntrinsics->findAndInclude(type.name())) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04002737 fProgramElements->push_back(found->clone());
2738 return this->convertTypeField(offset, type, field);
2739 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -05002740 }
Brian Osman1313d1a2020-09-08 10:34:30 -04002741 fErrors.error(offset,
Ethan Nicholase2c49992020-10-05 11:49:11 -04002742 "type '" + type.displayName() + "' does not have a member named '" + field +
2743 "'");
Brian Osman1313d1a2020-09-08 10:34:30 -04002744 return nullptr;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002745 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002746}
2747
Ethan Nicholasfc994162019-06-06 10:04:27 -04002748std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
2749 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
2750 auto iter = index.begin();
2751 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07002752 if (!base) {
2753 return nullptr;
2754 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002755 if (iter != index.end()) {
2756 return this->convertIndex(std::move(base), *(iter++));
Ethan Nicholase6592142020-09-08 10:22:09 -04002757 } else if (base->kind() == Expression::Kind::kTypeReference) {
Ethan Nicholas5194a702020-10-12 11:12:12 -04002758 const Type& oldType = base->as<TypeReference>().value();
John Stiles3ae071e2020-08-05 15:29:29 -04002759 const Type* newType = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Type>(
Brian Osmane8c26082020-10-01 17:22:45 -04002760 oldType.name() + "[]", Type::TypeKind::kArray, oldType, Type::kUnsizedArray));
Ethan Nicholase6592142020-09-08 10:22:09 -04002761 return std::make_unique<TypeReference>(fContext, base->fOffset, newType);
ethannicholasb3058bd2016-07-01 08:22:01 -07002762 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002763 fErrors.error(index.fOffset, "'[]' must follow a type name");
2764 return nullptr;
2765}
2766
2767std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
2768 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
2769 auto iter = callNode.begin();
2770 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
2771 if (!base) {
2772 return nullptr;
2773 }
John Stiles8e3b6be2020-10-13 11:14:08 -04002774 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04002775 for (; iter != callNode.end(); ++iter) {
2776 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
2777 if (!converted) {
2778 return nullptr;
2779 }
2780 arguments.push_back(std::move(converted));
2781 }
2782 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
2783}
2784
2785std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
2786 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
2787 if (!base) {
2788 return nullptr;
2789 }
2790 StringFragment field = fieldNode.getString();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002791 const Type& baseType = base->type();
2792 if (baseType == *fContext.fSkCaps_Type) {
Ethan Nicholas01ec7e82020-10-08 12:10:12 -04002793 const Type* type = this->typeForSetting(fieldNode.fOffset, field);
2794 if (!type) {
2795 return nullptr;
2796 }
2797 return std::make_unique<Setting>(fieldNode.fOffset, field, type);
Ethan Nicholasfc994162019-06-06 10:04:27 -04002798 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002799 if (base->kind() == Expression::Kind::kExternalValue) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002800 return this->convertField(std::move(base), field);
2801 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002802 switch (baseType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002803 case Type::TypeKind::kOther:
2804 case Type::TypeKind::kStruct:
Ethan Nicholasfc994162019-06-06 10:04:27 -04002805 return this->convertField(std::move(base), field);
2806 default:
Ethan Nicholas7b9da252020-08-03 13:43:50 -04002807 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04002808 }
2809}
2810
Brian Osman6518d772020-09-10 16:50:06 -04002811std::unique_ptr<Expression> IRGenerator::convertScopeExpression(const ASTNode& scopeNode) {
2812 std::unique_ptr<Expression> base = this->convertExpression(*scopeNode.begin());
2813 if (!base) {
2814 return nullptr;
2815 }
2816 if (!base->is<TypeReference>()) {
2817 fErrors.error(scopeNode.fOffset, "'::' must follow a type name");
2818 return nullptr;
2819 }
2820 StringFragment member = scopeNode.getString();
Ethan Nicholas5194a702020-10-12 11:12:12 -04002821 return this->convertTypeField(base->fOffset, base->as<TypeReference>().value(), member);
Brian Osman6518d772020-09-10 16:50:06 -04002822}
2823
Ethan Nicholasfc994162019-06-06 10:04:27 -04002824std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
2825 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
2826 if (!base) {
2827 return nullptr;
2828 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002829 const Type& baseType = base->type();
2830 if (!baseType.isNumber()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002831 fErrors.error(expression.fOffset,
2832 "'" + String(Compiler::OperatorName(expression.getToken().fKind)) +
Ethan Nicholas30d30222020-09-11 12:27:26 -04002833 "' cannot operate on '" + baseType.displayName() + "'");
Ethan Nicholasfc994162019-06-06 10:04:27 -04002834 return nullptr;
2835 }
Ethan Nicholas453f67f2020-10-09 10:43:45 -04002836 if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) {
John Stiles978674a2020-09-23 15:24:51 -04002837 return nullptr;
2838 }
2839 return std::make_unique<PostfixExpression>(std::move(base), expression.getToken().fKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002840}
2841
2842void IRGenerator::checkValid(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002843 switch (expr.kind()) {
2844 case Expression::Kind::kFunctionReference:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002845 fErrors.error(expr.fOffset, "expected '(' to begin function call");
ethannicholasb3058bd2016-07-01 08:22:01 -07002846 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002847 case Expression::Kind::kTypeReference:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002848 fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation");
ethannicholasb3058bd2016-07-01 08:22:01 -07002849 break;
2850 default:
Ethan Nicholas30d30222020-09-11 12:27:26 -04002851 if (expr.type() == *fContext.fInvalid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002852 fErrors.error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07002853 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002854 }
2855}
2856
John Stilesdce4d3e2020-09-25 14:35:13 -04002857bool IRGenerator::setRefKind(Expression& expr, VariableReference::RefKind kind) {
John Stilesa976da72020-09-25 23:06:26 -04002858 VariableReference* assignableVar = nullptr;
2859 if (!Analysis::IsAssignable(expr, &assignableVar, &fErrors)) {
John Stilesdce4d3e2020-09-25 14:35:13 -04002860 return false;
2861 }
John Stilesa976da72020-09-25 23:06:26 -04002862 if (assignableVar) {
2863 assignableVar->setRefKind(kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002864 }
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002865 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07002866}
2867
Brian Osman9496fe52020-11-18 14:48:19 -05002868void IRGenerator::findAndDeclareBuiltinVariables() {
2869 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04002870 public:
Brian Osman9496fe52020-11-18 14:48:19 -05002871 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04002872
Brian Osman9496fe52020-11-18 14:48:19 -05002873 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04002874 // If this is the *first* time we've seen this builtin, findAndInclude will return
2875 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05002876 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
2877 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<InterfaceBlock>());
2878 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04002879 }
2880 }
2881
Brian Osman9496fe52020-11-18 14:48:19 -05002882 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04002883 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Brian Osman9496fe52020-11-18 14:48:19 -05002884 this->addDeclaringElement(e.as<VariableReference>().variable()->name());
Brian Osman8e2ef022020-09-30 13:26:43 -04002885 }
Brian Osman8e2ef022020-09-30 13:26:43 -04002886 return INHERITED::visitExpression(e);
2887 }
2888
2889 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05002890 std::vector<const ProgramElement*> fNewElements;
Brian Osman8e2ef022020-09-30 13:26:43 -04002891
Brian Osman9496fe52020-11-18 14:48:19 -05002892 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04002893 using INHERITED::visitProgramElement;
2894 };
2895
Brian Osman9496fe52020-11-18 14:48:19 -05002896 BuiltinVariableScanner scanner(this);
Brian Osman00a8b5b2020-10-02 09:06:04 -04002897 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05002898 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04002899 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04002900
2901 // Vulkan requires certain builtin variables be present, even if they're unused. At one time,
2902 // validation errors would result if they were missing. Now, it's just (Adreno) driver bugs
2903 // that drop or corrupt draws if they're missing.
2904 switch (fKind) {
2905 case Program::kFragment_Kind:
Brian Osman9496fe52020-11-18 14:48:19 -05002906 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04002907 break;
2908 default:
2909 break;
2910 }
2911
Brian Osman9496fe52020-11-18 14:48:19 -05002912 fSharedElements->insert(
2913 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04002914}
2915
Brian Osman88cda172020-10-09 12:05:16 -04002916IRGenerator::IRBundle IRGenerator::convertProgram(
2917 Program::Kind kind,
2918 const Program::Settings* settings,
2919 const ParsedModule& base,
2920 bool isBuiltinCode,
2921 const char* text,
2922 size_t length,
2923 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
Robert Phillipsfe8da172018-01-24 14:52:02 +00002924 fKind = kind;
Brian Osman88cda172020-10-09 12:05:16 -04002925 fSettings = settings;
2926 fSymbolTable = base.fSymbols;
2927 fIntrinsics = base.fIntrinsics.get();
2928 if (fIntrinsics) {
2929 fIntrinsics->resetAlreadyIncluded();
2930 }
2931 fIsBuiltinCode = isBuiltinCode;
2932
2933 std::vector<std::unique_ptr<ProgramElement>> elements;
Brian Osman133724c2020-10-28 14:14:39 -04002934 std::vector<const ProgramElement*> sharedElements;
2935
Brian Osman88cda172020-10-09 12:05:16 -04002936 fProgramElements = &elements;
Brian Osman133724c2020-10-28 14:14:39 -04002937 fSharedElements = &sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04002938
2939 fInputs.reset();
2940 fInvocations = -1;
2941 fRTAdjust = nullptr;
2942 fRTAdjustInterfaceBlock = nullptr;
2943
John Stilese51b6a32020-10-21 15:02:37 -04002944 AutoSymbolTable table(this);
Brian Osman88cda172020-10-09 12:05:16 -04002945
Brian Osman68c1d402020-10-12 16:36:38 -04002946 if (kind == Program::kGeometry_Kind && !fIsBuiltinCode) {
2947 // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin.
2948 // If we're applying the workaround, then it's a plain global.
Brian Osmand7e76592020-11-02 12:26:22 -05002949 bool workaround = fCaps && !fCaps->gsInvocationsSupport();
Brian Osman68c1d402020-10-12 16:36:38 -04002950 Modifiers m;
2951 if (!workaround) {
2952 m.fFlags = Modifiers::kIn_Flag;
2953 m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN;
2954 }
John Stiles586df952020-11-12 18:27:13 -05002955 auto var = std::make_unique<Variable>(-1, fModifiers->addToPool(m), "sk_InvocationID",
Brian Osman68c1d402020-10-12 16:36:38 -04002956 fContext.fInt_Type.get(), false,
2957 Variable::Storage::kGlobal);
John Stiles87ae34e2020-10-13 12:50:11 -04002958 auto decl = std::make_unique<VarDeclaration>(var.get(), fContext.fInt_Type.get(),
John Stiles62a56462020-12-03 10:41:58 -05002959 /*arraySize=*/0, /*value=*/nullptr);
Brian Osman68c1d402020-10-12 16:36:38 -04002960 fSymbolTable->add(std::move(var));
2961 fProgramElements->push_back(
2962 std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl)));
2963 }
2964
Brian Osman88cda172020-10-09 12:05:16 -04002965 if (externalValues) {
2966 // Add any external values to the new symbol table, so they're only visible to this Program
2967 for (const auto& ev : *externalValues) {
2968 fSymbolTable->addWithoutOwnership(ev.get());
2969 }
2970 }
2971
Ethan Nicholasc18bb512020-07-28 14:46:53 -04002972 Parser parser(text, length, *fSymbolTable, fErrors);
Ethan Nicholasba9a04f2020-11-06 09:28:04 -05002973 fFile = parser.compilationUnit();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002974 if (fErrors.errorCount()) {
Brian Osman88cda172020-10-09 12:05:16 -04002975 return {};
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002976 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002977 SkASSERT(fFile);
2978 for (const auto& decl : fFile->root()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002979 switch (decl.fKind) {
John Stiles7bd70332020-11-30 17:04:09 -05002980 case ASTNode::Kind::kVarDeclarations:
2981 this->convertGlobalVarDeclarations(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002982 break;
John Stiles7bd70332020-11-30 17:04:09 -05002983
2984 case ASTNode::Kind::kEnum:
Ethan Nicholasfc994162019-06-06 10:04:27 -04002985 this->convertEnum(decl);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002986 break;
John Stiles7bd70332020-11-30 17:04:09 -05002987
2988 case ASTNode::Kind::kFunction:
Ethan Nicholasfc994162019-06-06 10:04:27 -04002989 this->convertFunction(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002990 break;
John Stiles7bd70332020-11-30 17:04:09 -05002991
Ethan Nicholasfc994162019-06-06 10:04:27 -04002992 case ASTNode::Kind::kModifiers: {
2993 std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002994 if (f) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002995 fProgramElements->push_back(std::move(f));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002996 }
2997 break;
2998 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002999 case ASTNode::Kind::kInterfaceBlock: {
3000 std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003001 if (i) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05003002 fProgramElements->push_back(std::move(i));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003003 }
3004 break;
3005 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04003006 case ASTNode::Kind::kExtension: {
3007 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
3008 decl.getString());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003009 if (e) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05003010 fProgramElements->push_back(std::move(e));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003011 }
3012 break;
3013 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04003014 case ASTNode::Kind::kSection: {
3015 std::unique_ptr<Section> s = this->convertSection(decl);
Ethan Nicholas762466e2017-06-29 10:03:38 -04003016 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05003017 fProgramElements->push_back(std::move(s));
Ethan Nicholas762466e2017-06-29 10:03:38 -04003018 }
3019 break;
3020 }
John Stilesdc75a972020-11-25 16:24:55 -05003021 case ASTNode::Kind::kType: {
3022 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
3023 if (s) {
3024 fProgramElements->push_back(std::move(s));
3025 }
3026 break;
3027 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003028 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05003029#ifdef SK_DEBUG
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003030 ABORT("unsupported declaration: %s\n", decl.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05003031#endif
3032 break;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003033 }
3034 }
Brian Osmanc95d3a12020-09-09 10:56:27 -04003035
Brian Osman9496fe52020-11-18 14:48:19 -05003036 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04003037 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05003038 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04003039 }
Brian Osman8e2ef022020-09-30 13:26:43 -04003040
Brian Osmanc95d3a12020-09-09 10:56:27 -04003041 // Do a final pass looking for dangling FunctionReference or TypeReference expressions
3042 class FindIllegalExpressions : public ProgramVisitor {
3043 public:
3044 FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {}
3045
3046 bool visitExpression(const Expression& e) override {
3047 fGenerator->checkValid(e);
3048 return INHERITED::visitExpression(e);
3049 }
3050
3051 IRGenerator* fGenerator;
3052 using INHERITED = ProgramVisitor;
3053 using INHERITED::visitProgramElement;
3054 };
3055 for (const auto& pe : *fProgramElements) {
3056 FindIllegalExpressions{this}.visitProgramElement(*pe);
3057 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003058
Brian Osman88cda172020-10-09 12:05:16 -04003059 fSettings = nullptr;
3060
Brian Osman133724c2020-10-28 14:14:39 -04003061 return IRBundle{std::move(elements), std::move(sharedElements), this->releaseModifiers(),
3062 fSymbolTable, fInputs};
Brian Osman88cda172020-10-09 12:05:16 -04003063}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04003064
John Stilesa6841be2020-08-06 14:11:56 -04003065} // namespace SkSL