blob: 56153fbc7293876c9d6731f9b8036c4539df9d17 [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
Ethan Nicholasdaed2592021-03-04 14:30:25 -050015#include "include/private/SkSLLayout.h"
John Stiles6bef6a72020-12-02 14:26:04 -050016#include "include/private/SkTArray.h"
Ethan Nicholasbe65cd52021-05-18 12:09:01 -040017#include "include/sksl/DSLCore.h"
John Stilesb2705a12021-02-19 10:55:14 -050018#include "src/core/SkScopeExit.h"
Ethan Nicholas6e0fa402020-08-20 14:08:23 -040019#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/SkSLCompiler.h"
John Stilesdc8ec312021-01-11 11:05:21 -050021#include "src/sksl/SkSLConstantFolder.h"
Brian Osman00185012021-02-04 16:07:11 -050022#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLParser.h"
Brian Osman3000d6b2020-07-31 15:57:28 -040024#include "src/sksl/SkSLUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLBinaryExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/sksl/ir/SkSLBreakStatement.h"
27#include "src/sksl/ir/SkSLConstructor.h"
28#include "src/sksl/ir/SkSLContinueStatement.h"
29#include "src/sksl/ir/SkSLDiscardStatement.h"
30#include "src/sksl/ir/SkSLDoStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040032#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050033#include "src/sksl/ir/SkSLExternalFunctionReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLField.h"
35#include "src/sksl/ir/SkSLFieldAccess.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/sksl/ir/SkSLForStatement.h"
37#include "src/sksl/ir/SkSLFunctionCall.h"
38#include "src/sksl/ir/SkSLFunctionDeclaration.h"
39#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050040#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLFunctionReference.h"
42#include "src/sksl/ir/SkSLIfStatement.h"
43#include "src/sksl/ir/SkSLIndexExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/ir/SkSLInterfaceBlock.h"
John Stiles7591d4b2021-09-13 13:32:06 -040045#include "src/sksl/ir/SkSLLiteral.h"
Brian Osman3099f792021-09-01 13:12:16 -040046#include "src/sksl/ir/SkSLMethodReference.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060047#include "src/sksl/ir/SkSLNop.h"
John Stilesff6c5bf2021-09-07 15:22:02 -040048#include "src/sksl/ir/SkSLPoison.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/sksl/ir/SkSLPostfixExpression.h"
50#include "src/sksl/ir/SkSLPrefixExpression.h"
51#include "src/sksl/ir/SkSLReturnStatement.h"
52#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050053#include "src/sksl/ir/SkSLStructDefinition.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/sksl/ir/SkSLSwitchCase.h"
55#include "src/sksl/ir/SkSLSwitchStatement.h"
56#include "src/sksl/ir/SkSLSwizzle.h"
57#include "src/sksl/ir/SkSLTernaryExpression.h"
58#include "src/sksl/ir/SkSLUnresolvedFunction.h"
59#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050060#include "src/sksl/ir/SkSLVariable.h"
61#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070062
63namespace SkSL {
64
John Stilesc1a98b82021-02-24 13:35:02 -050065IRGenerator::IRGenerator(const Context* context)
John Stiles10d39d92021-05-04 16:13:14 -040066 : fContext(*context) {}
Brian Osman0006ad02020-11-18 15:38:39 -050067
Brian Osmancc914522021-09-24 18:58:37 +000068std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
John Stilesd1204642021-02-17 16:30:02 -050069 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -040070 this->programKind() != ProgramKind::kVertex) {
Brian Osmancc914522021-09-24 18:58:37 +000071 this->errorReporter().error(offset, "extensions are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -040072 return nullptr;
73 }
74
Brian Osmancc914522021-09-24 18:58:37 +000075 return std::make_unique<Extension>(offset, name);
ethannicholasb3058bd2016-07-01 08:22:01 -070076}
77
John Stiles20c09f92021-03-12 09:25:04 -050078std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -070079 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -040080 case ASTNode::Kind::kBlock:
81 return this->convertBlock(statement);
82 case ASTNode::Kind::kVarDeclarations:
83 return this->convertVarDeclarationStatement(statement);
84 case ASTNode::Kind::kIf:
85 return this->convertIf(statement);
86 case ASTNode::Kind::kFor:
87 return this->convertFor(statement);
88 case ASTNode::Kind::kWhile:
89 return this->convertWhile(statement);
90 case ASTNode::Kind::kDo:
91 return this->convertDo(statement);
92 case ASTNode::Kind::kSwitch:
93 return this->convertSwitch(statement);
94 case ASTNode::Kind::kReturn:
95 return this->convertReturn(statement);
96 case ASTNode::Kind::kBreak:
97 return this->convertBreak(statement);
98 case ASTNode::Kind::kContinue:
99 return this->convertContinue(statement);
100 case ASTNode::Kind::kDiscard:
101 return this->convertDiscard(statement);
John Stilesdc75a972020-11-25 16:24:55 -0500102 case ASTNode::Kind::kType:
103 // TODO: add IRNode for struct definition inside a function
104 return nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400105 default:
106 // it's an expression
Brian Osman99ddd2a2021-08-27 11:21:12 -0400107 return this->convertExpressionStatement(statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 }
109}
110
Ethan Nicholasfc994162019-06-06 10:04:27 -0400111std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
112 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
John Stilesde2b6092021-09-17 10:40:47 -0400113 AutoSymbolTable table(&fSymbolTable);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400114 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400115 for (const auto& child : block) {
116 std::unique_ptr<Statement> statement = this->convertStatement(child);
John Stilesff6c5bf2021-09-07 15:22:02 -0400117 if (statement) {
118 statements.push_back(std::move(statement));
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 }
Brian Osmancc914522021-09-24 18:58:37 +0000121 return Block::Make(block.fOffset, std::move(statements), fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700122}
123
Ethan Nicholasfc994162019-06-06 10:04:27 -0400124std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
125 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400126 auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal);
Brian Osmanc0213602020-10-06 14:43:32 -0400127 if (decls.empty()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 return nullptr;
129 }
Brian Osmancc914522021-09-24 18:58:37 +0000130 return Block::MakeUnscoped(s.fOffset, std::move(decls));
ethannicholasb3058bd2016-07-01 08:22:01 -0700131}
132
Brian Osmancc914522021-09-24 18:58:37 +0000133int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) {
Brian Osman4cf85072021-01-14 14:18:10 -0500134 if (!s) {
Brian Osmancc914522021-09-24 18:58:37 +0000135 this->errorReporter().error(offset, "array must have a size");
Brian Osman4cf85072021-01-14 14:18:10 -0500136 return 0;
137 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500138 auto size = this->convertExpression(s);
139 if (!size) {
140 return 0;
141 }
John Stiles7253a202021-08-23 09:24:33 -0400142 return type.convertArraySize(fContext, std::move(size));
Brian Osman4cf85072021-01-14 14:18:10 -0500143}
144
Brian Osmancc914522021-09-24 18:58:37 +0000145void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType,
Ethan Nicholas489e5522021-01-20 10:53:11 -0500146 Variable::Storage storage) {
John Stilese6095982021-02-04 12:42:01 -0500147 if (this->strictES2Mode() && baseType->isArray()) {
Brian Osmancc914522021-09-24 18:58:37 +0000148 this->errorReporter().error(offset, "array size must appear after variable name");
John Stilese6095982021-02-04 12:42:01 -0500149 }
150
John Stiles3624aba2020-12-22 16:29:53 -0500151 if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) {
John Stilesb30151e2021-01-11 16:13:08 -0500152 this->errorReporter().error(
Brian Osmancc914522021-09-24 18:58:37 +0000153 offset,
John Stilesb30151e2021-01-11 16:13:08 -0500154 "variables of type '" + baseType->displayName() + "' must be global");
Brian Osman82329002020-07-21 09:39:27 -0400155 }
Brian Osman8c264792021-07-01 16:41:27 -0400156 if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) {
Brian Osmancc914522021-09-24 18:58:37 +0000157 this->errorReporter().error(offset, "'in' variables may not have matrix type");
Brian Osman8c264792021-07-01 16:41:27 -0400158 }
159 if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
Brian Osmancc914522021-09-24 18:58:37 +0000160 this->errorReporter().error(offset, "'in uniform' variables not permitted");
Brian Osman2fe83fe2019-12-16 13:17:59 -0500161 }
John Stilesbb2ef922021-07-26 08:32:07 -0400162 if (this->isRuntimeEffect()) {
Brian Osman5c625822021-04-06 14:28:07 -0400163 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osmancc914522021-09-24 18:58:37 +0000164 this->errorReporter().error(offset, "'in' variables not permitted in runtime effects");
Brian Osmana4b91692020-08-10 14:26:16 -0400165 }
166 }
Brian Osman5c625822021-04-06 14:28:07 -0400167 if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) {
168 this->errorReporter().error(
Brian Osmancc914522021-09-24 18:58:37 +0000169 offset, "variables of type '" + baseType->displayName() + "' must be uniform");
Brian Osman5c625822021-04-06 14:28:07 -0400170 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400171 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
John Stilesbb2ef922021-07-26 08:32:07 -0400172 if (!this->isRuntimeEffect()) {
Brian Osmancc914522021-09-24 18:58:37 +0000173 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500174 "'srgb_unpremul' is only permitted in runtime effects");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400175 }
176 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
Brian Osmancc914522021-09-24 18:58:37 +0000177 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500178 "'srgb_unpremul' is only permitted on 'uniform' variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400179 }
180 auto validColorXformType = [](const Type& t) {
John Stiles9aeed132020-11-24 17:36:06 -0500181 return t.isVector() && t.componentType().isFloat() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400182 (t.columns() == 3 || t.columns() == 4);
183 };
John Stilesc0c51062020-12-03 17:16:29 -0500184 if (!validColorXformType(*baseType) && !(baseType->isArray() &&
Brian Osmanb32d66b2020-04-30 17:12:03 -0400185 validColorXformType(baseType->componentType()))) {
Brian Osmancc914522021-09-24 18:58:37 +0000186 this->errorReporter().error(offset,
John Stilesb30151e2021-01-11 16:13:08 -0500187 "'srgb_unpremul' is only permitted on half3, half4, "
188 "float3, or float4 variables");
Brian Osmanb32d66b2020-04-30 17:12:03 -0400189 }
190 }
John Stilesb806da42021-08-04 16:26:47 -0400191 int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag |
192 Modifiers::kLowp_Flag;
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400193 if (storage == Variable::Storage::kGlobal) {
Brian Osman9ca38ff2021-02-18 12:58:54 -0500194 permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag |
Brian Osmane49703f2021-04-19 11:15:24 -0400195 Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag;
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400196 }
Brian Osmana654faa2021-02-26 11:52:59 -0500197 // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags
Brian Osmancc914522021-09-24 18:58:37 +0000198 CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0);
Ethan Nicholas489e5522021-01-20 10:53:11 -0500199}
200
Brian Osmancc914522021-09-24 18:58:37 +0000201std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400202 const Type* baseType, skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500203 bool isArray,
204 std::unique_ptr<Expression> arraySize,
205 Variable::Storage storage) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500206 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
John Stilesd1204642021-02-17 16:30:02 -0500207 (modifiers.fFlags & Modifiers::kOut_Flag) &&
John Stilese67bd132021-03-19 18:39:25 -0400208 this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) {
Brian Osmancc914522021-09-24 18:58:37 +0000209 this->errorReporter().error(offset,
Ethan Nicholas489e5522021-01-20 10:53:11 -0500210 "out location=0, index=0 is reserved for sk_FragColor");
211 }
212 const Type* type = baseType;
213 int arraySizeValue = 0;
214 if (isArray) {
Ethan Nicholas489e5522021-01-20 10:53:11 -0500215 SkASSERT(arraySize);
John Stiles7253a202021-08-23 09:24:33 -0400216 arraySizeValue = type->convertArraySize(fContext, std::move(arraySize));
John Stiles80b02af2021-02-12 17:07:51 -0500217 if (!arraySizeValue) {
218 return {};
219 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500220 type = fSymbolTable->addArrayDimension(type, arraySizeValue);
221 }
Brian Osmancc914522021-09-24 18:58:37 +0000222 return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name,
John Stilesf2872e62021-05-04 11:38:43 -0400223 type, fIsBuiltinCode, storage);
Ethan Nicholasbd974002021-02-22 16:20:06 -0500224}
225
226std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400227 std::unique_ptr<Expression> value,
228 bool addToSymbolTable) {
John Stilese67bd132021-03-19 18:39:25 -0400229 std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(),
230 std::move(value));
231 if (!varDecl) {
232 return nullptr;
233 }
234
235 // Detect the declaration of magical variables.
236 if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) {
237 // Silently ignore duplicate definitions of `sk_FragColor`.
238 const Symbol* symbol = (*fSymbolTable)[var->name()];
239 if (symbol) {
Brian Osman7b361492021-02-25 11:25:30 -0500240 return nullptr;
241 }
John Stilese67bd132021-03-19 18:39:25 -0400242 } else if ((var->storage() == Variable::Storage::kGlobal ||
243 var->storage() == Variable::Storage::kInterfaceBlock) &&
244 var->name() == Compiler::RTADJUST_NAME) {
245 // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions.
246 if (fRTAdjust) {
Brian Osmancc914522021-09-24 18:58:37 +0000247 this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'");
Brian Osman7b361492021-02-25 11:25:30 -0500248 return nullptr;
249 }
John Stilese67bd132021-03-19 18:39:25 -0400250 if (var->type() != *fContext.fTypes.fFloat4) {
Brian Osmancc914522021-09-24 18:58:37 +0000251 this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'");
Brian Osmanfc4b9912021-03-05 16:45:06 -0500252 return nullptr;
253 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500254 fRTAdjust = var.get();
255 }
John Stilese67bd132021-03-19 18:39:25 -0400256
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400257 if (addToSymbolTable) {
258 fSymbolTable->add(std::move(var));
259 } else {
260 fSymbolTable->takeOwnershipOfSymbol(std::move(var));
261 }
John Stilese67bd132021-03-19 18:39:25 -0400262 return varDecl;
Ethan Nicholasbd974002021-02-22 16:20:06 -0500263}
264
Brian Osmancc914522021-09-24 18:58:37 +0000265std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500266 const Modifiers& modifiers,
267 const Type* baseType,
Ethan Nicholas962dec42021-06-10 13:06:39 -0400268 skstd::string_view name,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500269 bool isArray,
270 std::unique_ptr<Expression> arraySize,
271 std::unique_ptr<Expression> value,
272 Variable::Storage storage) {
Brian Osmancc914522021-09-24 18:58:37 +0000273 std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray,
Ethan Nicholasbd974002021-02-22 16:20:06 -0500274 std::move(arraySize), storage);
275 if (!var) {
276 return nullptr;
277 }
278 return this->convertVarDeclaration(std::move(var), std::move(value));
Ethan Nicholas489e5522021-01-20 10:53:11 -0500279}
280
281StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls,
282 Variable::Storage storage) {
283 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
284 auto declarationsIter = decls.begin();
285 const Modifiers& modifiers = declarationsIter++->getModifiers();
286 const ASTNode& rawType = *(declarationsIter++);
287 const Type* baseType = this->convertType(rawType);
288 if (!baseType) {
289 return {};
290 }
John Stilesb806da42021-08-04 16:26:47 -0400291 baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(),
Brian Osmancc914522021-09-24 18:58:37 +0000292 decls.fOffset);
John Stilesb806da42021-08-04 16:26:47 -0400293 if (!baseType) {
294 return {};
295 }
Ethan Nicholas489e5522021-01-20 10:53:11 -0500296
Brian Osmancc914522021-09-24 18:58:37 +0000297 this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400298
299 StatementArray varDecls;
John Stilesf621e232020-08-25 13:33:02 -0400300 for (; declarationsIter != decls.end(); ++declarationsIter) {
301 const ASTNode& varDecl = *declarationsIter;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400302 const ASTNode::VarData& varData = varDecl.getVarData();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500303 std::unique_ptr<Expression> arraySize;
304 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400305 auto iter = varDecl.begin();
Ethan Nicholas489e5522021-01-20 10:53:11 -0500306 if (iter != varDecl.end() && varData.fIsArray) {
John Stiles8f633ef2021-08-21 17:13:11 -0400307 if (!*iter) {
Brian Osmancc914522021-09-24 18:58:37 +0000308 this->errorReporter().error(decls.fOffset, "array must have a size");
John Stilesfabed8b2021-03-29 09:38:59 -0400309 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700310 }
John Stiles8f633ef2021-08-21 17:13:11 -0400311 arraySize = this->convertExpression(*iter++);
312 if (!arraySize) {
313 continue;
314 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700315 }
Brian Osman4cf85072021-01-14 14:18:10 -0500316 if (iter != varDecl.end()) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400317 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318 if (!value) {
John Stilesfabed8b2021-03-29 09:38:59 -0400319 continue;
ethannicholasb3058bd2016-07-01 08:22:01 -0700320 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700321 }
Brian Osmancc914522021-09-24 18:58:37 +0000322 std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset,
Ethan Nicholas489e5522021-01-20 10:53:11 -0500323 modifiers,
324 baseType,
325 varData.fName,
326 varData.fIsArray,
327 std::move(arraySize),
328 std::move(value),
329 storage);
330 if (varDeclStmt) {
331 varDecls.push_back(std::move(varDeclStmt));
ethannicholasf789b382016-08-03 12:43:36 -0700332 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700333 }
Brian Osmanc0213602020-10-06 14:43:32 -0400334 return varDecls;
ethannicholasb3058bd2016-07-01 08:22:01 -0700335}
336
Ethan Nicholasfc994162019-06-06 10:04:27 -0400337std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
John Stilesd1204642021-02-17 16:30:02 -0500338 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400339 this->programKind() != ProgramKind::kVertex) {
Brian Osmancc914522021-09-24 18:58:37 +0000340 this->errorReporter().error(m.fOffset,
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400341 "layout qualifiers are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400342 return nullptr;
343 }
344
Ethan Nicholasfc994162019-06-06 10:04:27 -0400345 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
346 Modifiers modifiers = m.getModifiers();
John Stilesf2872e62021-05-04 11:38:43 -0400347 return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700348}
349
John Stilesad2319f2020-09-02 15:01:47 -0400350std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
351 SkASSERT(n.fKind == ASTNode::Kind::kIf);
352 auto iter = n.begin();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500353 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
John Stilesad2319f2020-09-02 15:01:47 -0400354 if (!test) {
355 return nullptr;
356 }
357 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
358 if (!ifTrue) {
359 return nullptr;
360 }
John Stilesad2319f2020-09-02 15:01:47 -0400361 std::unique_ptr<Statement> ifFalse;
362 if (iter != n.end()) {
363 ifFalse = this->convertStatement(*(iter++));
364 if (!ifFalse) {
365 return nullptr;
366 }
John Stilesad2319f2020-09-02 15:01:47 -0400367 }
John Stiles5ede6e32021-01-11 13:00:49 -0500368 bool isStatic = n.getBool();
Brian Osmancc914522021-09-24 18:58:37 +0000369 return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test),
John Stiles23521a82021-03-02 17:02:51 -0500370 std::move(ifTrue), std::move(ifFalse));
John Stilesad2319f2020-09-02 15:01:47 -0400371}
372
Ethan Nicholasfc994162019-06-06 10:04:27 -0400373std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
374 SkASSERT(f.fKind == ASTNode::Kind::kFor);
John Stilesde2b6092021-09-17 10:40:47 -0400375 AutoSymbolTable table(&fSymbolTable);
ethannicholas22f939e2016-10-13 13:25:34 -0700376 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400377 auto iter = f.begin();
378 if (*iter) {
379 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700380 if (!initializer) {
381 return nullptr;
382 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700383 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400384 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700385 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400386 if (*iter) {
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500387 test = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700388 if (!test) {
389 return nullptr;
390 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400392 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700393 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400394 if (*iter) {
395 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700396 if (!next) {
397 return nullptr;
398 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700399 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400400 ++iter;
401 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402 if (!statement) {
403 return nullptr;
404 }
Brian Osman77ba8102021-01-12 17:15:30 -0500405
Brian Osmancc914522021-09-24 18:58:37 +0000406 return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test),
John Stiles23521a82021-03-02 17:02:51 -0500407 std::move(next), std::move(statement), fSymbolTable);
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500408}
409
410std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
411 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 auto iter = w.begin();
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500413 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700414 if (!test) {
415 return nullptr;
416 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400417 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700418 if (!statement) {
419 return nullptr;
420 }
Brian Osmancc914522021-09-24 18:58:37 +0000421 return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement),
John Stiles23521a82021-03-02 17:02:51 -0500422 fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423}
424
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500425std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
426 SkASSERT(d.fKind == ASTNode::Kind::kDo);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400427 auto iter = d.begin();
428 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
429 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700430 return nullptr;
431 }
Ethan Nicholas2ed0d942021-01-20 07:51:23 -0500432 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400433 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700434 return nullptr;
435 }
John Stiles23521a82021-03-02 17:02:51 -0500436 return DoStatement::Convert(fContext, std::move(statement), std::move(test));
ethannicholasb3058bd2016-07-01 08:22:01 -0700437}
438
Ethan Nicholascfefec02021-02-09 15:22:57 -0500439std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
440 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
441
442 auto iter = s.begin();
443 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
444 if (!value) {
445 return nullptr;
446 }
John Stilesde2b6092021-09-17 10:40:47 -0400447 AutoSymbolTable table(&fSymbolTable);
Ethan Nicholascfefec02021-02-09 15:22:57 -0500448 ExpressionArray caseValues;
John Stilesc3ce43b2021-03-09 15:37:01 -0500449 StatementArray caseStatements;
Ethan Nicholascfefec02021-02-09 15:22:57 -0500450 for (; iter != s.end(); ++iter) {
451 const ASTNode& c = *iter;
452 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
453 std::unique_ptr<Expression>& caseValue = caseValues.emplace_back();
454 auto childIter = c.begin();
455 if (*childIter) {
456 caseValue = this->convertExpression(*childIter);
457 if (!caseValue) {
458 return nullptr;
459 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500460 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400461 ++childIter;
John Stilesc3ce43b2021-03-09 15:37:01 -0500462
John Stilesbf16b6c2021-03-12 19:24:31 -0500463 StatementArray statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400464 for (; childIter != c.end(); ++childIter) {
465 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500466 if (!converted) {
467 return nullptr;
468 }
469 statements.push_back(std::move(converted));
470 }
John Stilesbf16b6c2021-03-12 19:24:31 -0500471
Brian Osmancc914522021-09-24 18:58:37 +0000472 caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements)));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500473 }
Brian Osmancc914522021-09-24 18:58:37 +0000474 return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value),
John Stiles23521a82021-03-02 17:02:51 -0500475 std::move(caseValues), std::move(caseStatements), fSymbolTable);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500476}
477
Ethan Nicholasfc994162019-06-06 10:04:27 -0400478std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
479 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700480 if (!e) {
481 return nullptr;
482 }
John Stiles3e5871c2021-02-25 20:52:03 -0500483 return ExpressionStatement::Make(fContext, std::move(e));
ethannicholasb3058bd2016-07-01 08:22:01 -0700484}
485
Brian Osmancc914522021-09-24 18:58:37 +0000486std::unique_ptr<Statement> IRGenerator::convertReturn(int offset,
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500487 std::unique_ptr<Expression> result) {
Brian Osmancc914522021-09-24 18:58:37 +0000488 return ReturnStatement::Make(offset, std::move(result));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500489}
490
491std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
492 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
493 if (r.begin() != r.end()) {
John Stilesff6c5bf2021-09-07 15:22:02 -0400494 if (std::unique_ptr<Expression> value = this->convertExpression(*r.begin())) {
Brian Osmancc914522021-09-24 18:58:37 +0000495 return this->convertReturn(r.fOffset, std::move(value));
John Stilesff6c5bf2021-09-07 15:22:02 -0400496 } else {
Brian Osmancc914522021-09-24 18:58:37 +0000497 return this->convertReturn(r.fOffset, Poison::Make(r.fOffset, fContext));
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500498 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700499 }
John Stilesff6c5bf2021-09-07 15:22:02 -0400500
Brian Osmancc914522021-09-24 18:58:37 +0000501 return this->convertReturn(r.fOffset, /*result=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700502}
503
Ethan Nicholasfc994162019-06-06 10:04:27 -0400504std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
505 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
Brian Osmancc914522021-09-24 18:58:37 +0000506 return BreakStatement::Make(b.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700507}
508
Ethan Nicholasfc994162019-06-06 10:04:27 -0400509std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
510 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
Brian Osmancc914522021-09-24 18:58:37 +0000511 return ContinueStatement::Make(c.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700512}
513
Ethan Nicholasfc994162019-06-06 10:04:27 -0400514std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
515 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Brian Osman8c264792021-07-01 16:41:27 -0400516 if (this->programKind() != ProgramKind::kFragment) {
Brian Osmancc914522021-09-24 18:58:37 +0000517 this->errorReporter().error(d.fOffset,
John Stilesb30151e2021-01-11 16:13:08 -0500518 "discard statement is only permitted in fragment shaders");
Brian Osmanf2876b02020-12-15 15:40:32 -0500519 return nullptr;
520 }
Brian Osmancc914522021-09-24 18:58:37 +0000521 return DiscardStatement::Make(d.fOffset);
ethannicholasb3058bd2016-07-01 08:22:01 -0700522}
523
John Stiles3b204892021-08-27 17:35:35 -0400524void IRGenerator::appendRTAdjustFixupToVertexMain(const FunctionDeclaration& decl, Block* body) {
John Stilesd45ebbc2021-05-18 16:54:36 -0400525 using namespace SkSL::dsl;
526 using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle
John Stiles3b204892021-08-27 17:35:35 -0400527 using OwnerKind = SkSL::FieldAccess::OwnerKind;
John Stilesd45ebbc2021-05-18 16:54:36 -0400528
John Stiles3b204892021-08-27 17:35:35 -0400529 // If this is a vertex program that uses RTAdjust, and this is main()...
Ethan Nicholasa97ad0f2021-09-02 09:54:04 -0400530 if ((fRTAdjust || fRTAdjustInterfaceBlock) && decl.isMain() &&
531 ProgramKind::kVertex == this->programKind()) {
John Stiles3b204892021-08-27 17:35:35 -0400532 // ... append a line to the end of the function body which fixes up sk_Position.
533 const Variable* skPerVertex = nullptr;
534 if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) {
535 SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>());
536 skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable();
537 }
538
539 SkASSERT(skPerVertex);
540 auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
Brian Osmancc914522021-09-24 18:58:37 +0000541 return VariableReference::Make(/*offset=*/-1, var);
John Stiles3b204892021-08-27 17:35:35 -0400542 };
543 auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
544 return FieldAccess::Make(fContext, Ref(var), idx, OwnerKind::kAnonymousInterfaceBlock);
545 };
546 auto Pos = [&]() -> DSLExpression {
547 return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0,
548 OwnerKind::kAnonymousInterfaceBlock));
549 };
550 auto Adjust = [&]() -> DSLExpression {
551 return DSLExpression(fRTAdjustInterfaceBlock
552 ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex)
553 : Ref(fRTAdjust));
554 };
555
556 auto fixupStmt = DSLStatement(
557 Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
558 Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
559 0,
560 Pos().w())
561 );
562
563 body->children().push_back(fixupStmt.release());
Brian Osman88cda172020-10-09 12:05:16 -0400564 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000565}
566
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400567void IRGenerator::CheckModifiers(const Context& context,
Brian Osmancc914522021-09-24 18:58:37 +0000568 int offset,
Brian Osmana654faa2021-02-26 11:52:59 -0500569 const Modifiers& modifiers,
570 int permittedModifierFlags,
571 int permittedLayoutFlags) {
Brian Osmanb5de6be2021-08-10 15:16:34 -0400572 static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = {
573 { Modifiers::kConst_Flag, "const" },
574 { Modifiers::kIn_Flag, "in" },
575 { Modifiers::kOut_Flag, "out" },
576 { Modifiers::kUniform_Flag, "uniform" },
577 { Modifiers::kFlat_Flag, "flat" },
578 { Modifiers::kNoPerspective_Flag, "noperspective" },
579 { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" },
580 { Modifiers::kInline_Flag, "inline" },
581 { Modifiers::kNoInline_Flag, "noinline" },
582 { Modifiers::kHighp_Flag, "highp" },
583 { Modifiers::kMediump_Flag, "mediump" },
584 { Modifiers::kLowp_Flag, "lowp" },
John Stilesefde90d2021-08-12 23:06:24 -0400585 { Modifiers::kES3_Flag, "$es3" },
Brian Osmana654faa2021-02-26 11:52:59 -0500586 };
587
Brian Osmanb5de6be2021-08-10 15:16:34 -0400588 int modifierFlags = modifiers.fFlags;
589 for (const auto& f : kModifierFlags) {
590 if (modifierFlags & f.flag) {
591 if (!(permittedModifierFlags & f.flag)) {
Brian Osmancc914522021-09-24 18:58:37 +0000592 context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here");
Brian Osmanb5de6be2021-08-10 15:16:34 -0400593 }
594 modifierFlags &= ~f.flag;
595 }
596 }
597 SkASSERT(modifierFlags == 0);
598
599 static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = {
600 { Layout::kOriginUpperLeft_Flag, "origin_upper_left"},
601 { Layout::kPushConstant_Flag, "push_constant"},
602 { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"},
603 { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"},
604 { Layout::kLocation_Flag, "location"},
605 { Layout::kOffset_Flag, "offset"},
606 { Layout::kBinding_Flag, "binding"},
607 { Layout::kIndex_Flag, "index"},
608 { Layout::kSet_Flag, "set"},
609 { Layout::kBuiltin_Flag, "builtin"},
610 { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"},
Brian Osmanb5de6be2021-08-10 15:16:34 -0400611 };
Brian Osmana654faa2021-02-26 11:52:59 -0500612
613 int layoutFlags = modifiers.fLayout.fFlags;
Brian Osmanb5de6be2021-08-10 15:16:34 -0400614 for (const auto& lf : kLayoutFlags) {
615 if (layoutFlags & lf.flag) {
616 if (!(permittedLayoutFlags & lf.flag)) {
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400617 context.fErrors->error(
Brian Osmancc914522021-09-24 18:58:37 +0000618 offset, "layout qualifier '" + String(lf.name) + "' is not permitted here");
Brian Osmana654faa2021-02-26 11:52:59 -0500619 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400620 layoutFlags &= ~lf.flag;
Brian Osmana654faa2021-02-26 11:52:59 -0500621 }
Brian Osmanb5de6be2021-08-10 15:16:34 -0400622 }
Brian Osmana654faa2021-02-26 11:52:59 -0500623 SkASSERT(layoutFlags == 0);
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400624}
625
Ethan Nicholasfc994162019-06-06 10:04:27 -0400626void IRGenerator::convertFunction(const ASTNode& f) {
627 auto iter = f.begin();
Brian Osmand8070392020-09-09 15:50:02 -0400628 const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true);
John Stilesb9af7232020-08-20 15:57:48 -0400629 if (returnType == nullptr) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400630 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700631 }
John Stilesb9af7232020-08-20 15:57:48 -0400632 const ASTNode::FunctionData& funcData = f.getFunctionData();
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400633 std::vector<std::unique_ptr<Variable>> parameters;
634 parameters.reserve(funcData.fParameterCount);
John Stilesb9af7232020-08-20 15:57:48 -0400635 for (size_t i = 0; i < funcData.fParameterCount; ++i) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400636 const ASTNode& param = *(iter++);
637 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
John Stilesece1bf02021-03-08 11:15:55 -0500638 const ASTNode::ParameterData& pd = param.getParameterData();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400639 auto paramIter = param.begin();
640 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700641 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400642 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700643 }
John Stilesd39aec92020-12-03 14:37:16 -0500644 if (pd.fIsArray) {
Brian Osmancc914522021-09-24 18:58:37 +0000645 int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++);
Brian Osman4cf85072021-01-14 14:18:10 -0500646 if (!arraySize) {
647 return;
648 }
John Stilesa2179502020-12-03 14:37:57 -0500649 type = fSymbolTable->addArrayDimension(type, arraySize);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 }
Brian Osman8dbdf232020-10-12 14:40:24 -0400651
Brian Osmancc914522021-09-24 18:58:37 +0000652 parameters.push_back(std::make_unique<Variable>(param.fOffset,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400653 this->modifiersPool().add(pd.fModifiers),
654 pd.fName,
655 type,
656 fIsBuiltinCode,
657 Variable::Storage::kParameter));
ethannicholasb3058bd2016-07-01 08:22:01 -0700658 }
659
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400660 // Conservatively assume all user-defined functions have side effects.
661 Modifiers declModifiers = funcData.fModifiers;
662 if (!fIsBuiltinCode) {
663 declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholas00543112018-07-31 09:44:36 -0400664 }
665
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400666 if (fContext.fConfig->fSettings.fForceNoInline) {
667 // Apply the `noinline` modifier to every function. This allows us to test Runtime
668 // Effects without any inlining, even when the code is later added to a paint.
669 declModifiers.fFlags &= ~Modifiers::kInline_Flag;
670 declModifiers.fFlags |= Modifiers::kNoInline_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700671 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400672
673 const FunctionDeclaration* decl = FunctionDeclaration::Convert(
674 fContext,
675 *fSymbolTable,
Brian Osmancc914522021-09-24 18:58:37 +0000676 f.fOffset,
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400677 this->modifiersPool().add(declModifiers),
678 funcData.fName,
679 std::move(parameters),
680 returnType,
681 fIsBuiltinCode);
ethannicholasb3058bd2016-07-01 08:22:01 -0700682 if (!decl) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -0400683 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700684 }
John Stiles569249b2020-11-03 12:18:22 -0500685 if (iter == f.end()) {
686 // If there's no body, we've found a prototype.
Brian Osmancc914522021-09-24 18:58:37 +0000687 fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl,
John Stiles569249b2020-11-03 12:18:22 -0500688 fIsBuiltinCode));
689 } else {
690 // Compile function body.
John Stilesde2b6092021-09-17 10:40:47 -0400691 AutoSymbolTable table(&fSymbolTable);
John Stiles569249b2020-11-03 12:18:22 -0500692 for (const Variable* param : decl->parameters()) {
693 fSymbolTable->addWithoutOwnership(param);
ethannicholasb3058bd2016-07-01 08:22:01 -0700694 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400695 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700696 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400697 return;
698 }
John Stiles3b204892021-08-27 17:35:35 -0400699 this->appendRTAdjustFixupToVertexMain(*decl, body.get());
700 std::unique_ptr<FunctionDefinition> result = FunctionDefinition::Convert(
Brian Osmancc914522021-09-24 18:58:37 +0000701 fContext, f.fOffset, *decl, std::move(body), fIsBuiltinCode);
Ethan Nicholased84b732020-10-08 11:45:44 -0400702 decl->setDefinition(result.get());
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400703 result->setSource(&f);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500704 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700705 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700706}
707
John Stilesdc75a972020-11-25 16:24:55 -0500708std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) {
709 SkASSERT(node.fKind == ASTNode::Kind::kType);
710
711 const Type* type = this->convertType(node);
712 if (!type) {
713 return nullptr;
714 }
John Stilesc0c51062020-12-03 17:16:29 -0500715 if (!type->isStruct()) {
Brian Osmancc914522021-09-24 18:58:37 +0000716 this->errorReporter().error(node.fOffset,
John Stilesb30151e2021-01-11 16:13:08 -0500717 "expected a struct here, found '" + type->name() + "'");
John Stilesdc75a972020-11-25 16:24:55 -0500718 return nullptr;
719 }
Brian Osman02bc5222021-01-28 11:00:20 -0500720 SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type);
721 SkASSERT(wasInserted);
Brian Osmancc914522021-09-24 18:58:37 +0000722 return std::make_unique<StructDefinition>(node.fOffset, *type);
John Stilesdc75a972020-11-25 16:24:55 -0500723}
724
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400725void IRGenerator::scanInterfaceBlock(SkSL::InterfaceBlock& intf) {
726 const std::vector<Type::Field>& fields = intf.variable().type().componentType().fields();
727 for (size_t i = 0; i < fields.size(); ++i) {
728 const Type::Field& f = fields[i];
729 if (f.fName == Compiler::RTADJUST_NAME) {
730 if (*f.fType == *fContext.fTypes.fFloat4) {
731 fRTAdjustInterfaceBlock = &intf.variable();
732 fRTAdjustFieldIndex = i;
733 } else {
Brian Osmancc914522021-09-24 18:58:37 +0000734 this->errorReporter().error(intf.fOffset, "sk_RTAdjust must have type 'float4'");
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400735 }
736 }
737 }
738}
739
Ethan Nicholase6ed3c22021-07-08 10:38:43 -0400740std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
John Stilesd1204642021-02-17 16:30:02 -0500741 if (this->programKind() != ProgramKind::kFragment &&
Brian Osman99ddd2a2021-08-27 11:21:12 -0400742 this->programKind() != ProgramKind::kVertex) {
Brian Osmancc914522021-09-24 18:58:37 +0000743 this->errorReporter().error(intf.fOffset,
Ethan Nicholasa2c76c72021-09-07 15:21:13 -0400744 "interface blocks are not allowed in this kind of program");
Brian Osman16f376f2020-09-02 12:30:59 -0400745 return nullptr;
746 }
747
Ethan Nicholasfc994162019-06-06 10:04:27 -0400748 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
John Stilesece1bf02021-03-08 11:15:55 -0500749 const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700750 std::shared_ptr<SymbolTable> old = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400751 std::shared_ptr<SymbolTable> symbols;
ethannicholasb3058bd2016-07-01 08:22:01 -0700752 std::vector<Type::Field> fields;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400753 auto iter = intf.begin();
John Stiles869cdef2020-10-30 14:24:24 -0400754 {
John Stilesde2b6092021-09-17 10:40:47 -0400755 AutoSymbolTable table(&fSymbolTable);
John Stiles869cdef2020-10-30 14:24:24 -0400756 symbols = fSymbolTable;
John Stiles869cdef2020-10-30 14:24:24 -0400757 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
758 StatementArray decls = this->convertVarDeclarations(*(iter++),
759 Variable::Storage::kInterfaceBlock);
760 if (decls.empty()) {
761 return nullptr;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400762 }
John Stiles869cdef2020-10-30 14:24:24 -0400763 for (const auto& decl : decls) {
764 const VarDeclaration& vd = decl->as<VarDeclaration>();
John Stiles869cdef2020-10-30 14:24:24 -0400765 fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(),
766 &vd.var().type()));
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400767 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500768 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700769 }
Brian Osmancc914522021-09-24 18:58:37 +0000770 const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset,
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400771 id.fTypeName,
John Stilesad2d4942020-12-11 16:55:58 -0500772 fields));
John Stilesd39aec02020-12-03 10:42:26 -0500773 int arraySize = 0;
John Stilesd39aec92020-12-03 14:37:16 -0500774 if (id.fIsArray) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400775 const ASTNode& size = *(iter++);
Brian Osmancc914522021-09-24 18:58:37 +0000776 arraySize = this->convertArraySize(*type, size.fOffset, size);
Brian Osmanb42c3832021-08-27 15:19:03 -0400777 if (!arraySize) {
778 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500779 }
John Stilesa2179502020-12-03 14:37:57 -0500780 type = symbols->addArrayDimension(type, arraySize);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500781 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400782 const Variable* var = old->takeOwnershipOfSymbol(
Brian Osmancc914522021-09-24 18:58:37 +0000783 std::make_unique<Variable>(intf.fOffset,
John Stilesf2872e62021-05-04 11:38:43 -0400784 this->modifiersPool().add(id.fModifiers),
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400785 id.fInstanceName.length() ? id.fInstanceName : id.fTypeName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400786 type,
Brian Osman3887a012020-09-30 13:22:27 -0400787 fIsBuiltinCode,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400788 Variable::Storage::kGlobal));
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400789 if (id.fInstanceName.length()) {
John Stilesb8cc6652020-10-08 09:12:07 -0400790 old->addWithoutOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700791 } else {
792 for (size_t i = 0; i < fields.size(); i++) {
Brian Osmancc914522021-09-24 18:58:37 +0000793 old->add(std::make_unique<Field>(intf.fOffset, var, (int)i));
ethannicholasb3058bd2016-07-01 08:22:01 -0700794 }
795 }
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400796 std::unique_ptr<SkSL::InterfaceBlock> result = std::make_unique<SkSL::InterfaceBlock>(
Brian Osmancc914522021-09-24 18:58:37 +0000797 intf.fOffset, *var, id.fTypeName, id.fInstanceName, arraySize, symbols);
Ethan Nicholas494eb3e2021-08-27 19:17:01 -0400798 this->scanInterfaceBlock(*result);
799 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700800}
801
John Stiles7bd70332020-11-30 17:04:09 -0500802void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) {
803 StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal);
804 for (std::unique_ptr<Statement>& stmt : decls) {
Brian Osman02bc5222021-01-28 11:00:20 -0500805 const Type* type = &stmt->as<VarDeclaration>().baseType();
806 if (type->isStruct()) {
807 auto [iter, wasInserted] = fDefinedStructs.insert(type);
808 if (wasInserted) {
809 fProgramElements->push_back(
Brian Osmancc914522021-09-24 18:58:37 +0000810 std::make_unique<StructDefinition>(decl.fOffset, *type));
Brian Osman02bc5222021-01-28 11:00:20 -0500811 }
812 }
Ethan Nicholas624a5292021-04-16 14:54:43 -0400813 fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt)));
John Stiles7bd70332020-11-30 17:04:09 -0500814 }
815}
816
Brian Osmand8070392020-09-09 15:50:02 -0400817const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) {
Ethan Nicholas962dec42021-06-10 13:06:39 -0400818 skstd::string_view name = type.getStringView();
Brian Osman00fea5b2021-01-28 09:46:30 -0500819 const Symbol* symbol = (*fSymbolTable)[name];
John Stiles6bef6a72020-12-02 14:26:04 -0500820 if (!symbol || !symbol->is<Type>()) {
Brian Osmancc914522021-09-24 18:58:37 +0000821 this->errorReporter().error(type.fOffset, "unknown type '" + name + "'");
John Stiles7bd70332020-11-30 17:04:09 -0500822 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700823 }
John Stiles6bef6a72020-12-02 14:26:04 -0500824 const Type* result = &symbol->as<Type>();
John Stiles7bd70332020-11-30 17:04:09 -0500825 const bool isArray = (type.begin() != type.end());
John Stiles2558c462021-03-16 17:49:20 -0400826 if (result->isVoid() && !allowVoid) {
Brian Osmancc914522021-09-24 18:58:37 +0000827 this->errorReporter().error(type.fOffset,
John Stiles80b02af2021-02-12 17:07:51 -0500828 "type '" + name + "' not allowed in this context");
829 return nullptr;
John Stiles7bd70332020-11-30 17:04:09 -0500830 }
John Stiles823c5042021-08-17 12:09:00 -0400831 if (!fIsBuiltinCode) {
Ethan Nicholas517f4ff2021-09-02 10:57:45 -0400832 if (result->containsPrivateFields()) {
Brian Osmancc914522021-09-24 18:58:37 +0000833 this->errorReporter().error(type.fOffset, "type '" + name + "' is private");
John Stiles823c5042021-08-17 12:09:00 -0400834 return nullptr;
835 }
836 if (this->strictES2Mode() && !result->allowedInES2()) {
Brian Osmancc914522021-09-24 18:58:37 +0000837 this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported");
John Stiles823c5042021-08-17 12:09:00 -0400838 return nullptr;
839 }
John Stiles7bd70332020-11-30 17:04:09 -0500840 }
John Stiles6bef6a72020-12-02 14:26:04 -0500841 if (isArray) {
John Stilesd39aec92020-12-03 14:37:16 -0500842 auto iter = type.begin();
Brian Osmancc914522021-09-24 18:58:37 +0000843 int arraySize = this->convertArraySize(*result, type.fOffset, *iter);
Brian Osman4cf85072021-01-14 14:18:10 -0500844 if (!arraySize) {
845 return nullptr;
846 }
John Stilesa2179502020-12-03 14:37:57 -0500847 result = fSymbolTable->addArrayDimension(result, arraySize);
John Stiles7bd70332020-11-30 17:04:09 -0500848 }
John Stiles6bef6a72020-12-02 14:26:04 -0500849 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700850}
851
Ethan Nicholasfc994162019-06-06 10:04:27 -0400852std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700853 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400854 case ASTNode::Kind::kBinary:
855 return this->convertBinaryExpression(expr);
856 case ASTNode::Kind::kBool:
Brian Osmancc914522021-09-24 18:58:37 +0000857 return Literal::MakeBool(fContext, expr.fOffset, expr.getBool());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400858 case ASTNode::Kind::kCall:
859 return this->convertCallExpression(expr);
860 case ASTNode::Kind::kField:
861 return this->convertFieldExpression(expr);
862 case ASTNode::Kind::kFloat:
Brian Osmancc914522021-09-24 18:58:37 +0000863 return Literal::MakeFloat(fContext, expr.fOffset, expr.getFloat());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400864 case ASTNode::Kind::kIdentifier:
865 return this->convertIdentifier(expr);
866 case ASTNode::Kind::kIndex:
867 return this->convertIndexExpression(expr);
868 case ASTNode::Kind::kInt:
Brian Osmancc914522021-09-24 18:58:37 +0000869 return Literal::MakeInt(fContext, expr.fOffset, expr.getInt());
Ethan Nicholasfc994162019-06-06 10:04:27 -0400870 case ASTNode::Kind::kPostfix:
871 return this->convertPostfixExpression(expr);
872 case ASTNode::Kind::kPrefix:
873 return this->convertPrefixExpression(expr);
874 case ASTNode::Kind::kTernary:
875 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700876 default:
John Stilesfbb905e2021-08-25 15:51:50 -0400877 SkDEBUGFAIL("unsupported expression");
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500878 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700879 }
880}
881
Brian Osmancc914522021-09-24 18:58:37 +0000882std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500883 const Symbol* result = (*fSymbolTable)[name];
ethannicholasb3058bd2016-07-01 08:22:01 -0700884 if (!result) {
Brian Osmancc914522021-09-24 18:58:37 +0000885 this->errorReporter().error(offset, "unknown identifier '" + name + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700886 return nullptr;
887 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400888 switch (result->kind()) {
889 case Symbol::Kind::kFunctionDeclaration: {
ethannicholasd598f792016-07-25 10:08:54 -0700890 std::vector<const FunctionDeclaration*> f = {
John Stiles17c5b702020-08-18 10:40:03 -0400891 &result->as<FunctionDeclaration>()
ethannicholasb3058bd2016-07-01 08:22:01 -0700892 };
Brian Osmancc914522021-09-24 18:58:37 +0000893 return std::make_unique<FunctionReference>(fContext, offset, f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700894 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400895 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400896 const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
Brian Osmancc914522021-09-24 18:58:37 +0000897 return std::make_unique<FunctionReference>(fContext, offset, f->functions());
ethannicholasb3058bd2016-07-01 08:22:01 -0700898 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400899 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400900 const Variable* var = &result->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400901 const Modifiers& modifiers = var->modifiers();
902 switch (modifiers.fLayout.fBuiltin) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400903 case SK_FRAGCOORD_BUILTIN:
Brian Salomond8d85b92021-07-07 09:41:17 -0400904 if (caps().canUseFragCoord()) {
905 fInputs.fUseFlipRTUniform = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400906 }
Brian Salomond8d85b92021-07-07 09:41:17 -0400907 break;
908 case SK_CLOCKWISE_BUILTIN:
909 fInputs.fUseFlipRTUniform = true;
910 break;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400911 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500912 // default to kRead_RefKind; this will be corrected later if the variable is written to
Brian Osmancc914522021-09-24 18:58:37 +0000913 return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
ethannicholasb3058bd2016-07-01 08:22:01 -0700914 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400915 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400916 const Field* field = &result->as<Field>();
Brian Osmancc914522021-09-24 18:58:37 +0000917 auto base = VariableReference::Make(offset, &field->owner(),
John Stilesa94e0262021-04-27 17:29:59 -0400918 VariableReference::RefKind::kRead);
John Stiles06d600f2021-03-08 09:18:21 -0500919 return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
920 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700921 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400922 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400923 const Type* t = &result->as<Type>();
Brian Osmancc914522021-09-24 18:58:37 +0000924 return std::make_unique<TypeReference>(fContext, offset, t);
ethannicholasb3058bd2016-07-01 08:22:01 -0700925 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400926 case Symbol::Kind::kExternal: {
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500927 const ExternalFunction* r = &result->as<ExternalFunction>();
Brian Osmancc914522021-09-24 18:58:37 +0000928 return std::make_unique<ExternalFunctionReference>(offset, r);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400929 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700930 default:
John Stilesf57207b2021-02-02 17:50:34 -0500931 SK_ABORT("unsupported symbol type %d\n", (int) result->kind());
ethannicholasb3058bd2016-07-01 08:22:01 -0700932 }
Ethan Nicholasc0709392017-06-27 11:20:22 -0400933}
934
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500935std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
Brian Osmancc914522021-09-24 18:58:37 +0000936 return this->convertIdentifier(identifier.fOffset, identifier.getStringView());
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500937}
938
Ethan Nicholasfc994162019-06-06 10:04:27 -0400939std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
940 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
941 auto iter = expression.begin();
942 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700943 if (!left) {
944 return nullptr;
945 }
John Stiles0f464502020-11-20 12:52:22 -0500946 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700947 if (!right) {
948 return nullptr;
949 }
John Stiles23521a82021-03-02 17:02:51 -0500950 return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(),
951 std::move(right));
ethannicholasb3058bd2016-07-01 08:22:01 -0700952}
953
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500954std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
955 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
956 auto iter = node.begin();
957 std::unique_ptr<Expression> test = this->convertExpression(*(iter++));
958 if (!test) {
959 return nullptr;
960 }
961 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
962 if (!ifTrue) {
963 return nullptr;
964 }
965 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
966 if (!ifFalse) {
967 return nullptr;
968 }
John Stiles23521a82021-03-02 17:02:51 -0500969 return TernaryExpression::Convert(fContext, std::move(test),
970 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholas9ead3df2021-01-06 12:10:48 -0500971}
972
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400973void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {
Brian Osman00a8b5b2020-10-02 09:06:04 -0400974 if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) {
Brian Osman2b469eb2020-09-21 11:32:10 -0400975 const FunctionDefinition& original = found->as<FunctionDefinition>();
John Stiles9878d9e2020-09-22 15:40:16 -0400976
977 // Sort the referenced intrinsics into a consistent order; otherwise our output will become
978 // non-deterministic.
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400979 std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(),
980 original.referencedIntrinsics().end());
John Stiles9878d9e2020-09-22 15:40:16 -0400981 std::sort(intrinsics.begin(), intrinsics.end(),
982 [](const FunctionDeclaration* a, const FunctionDeclaration* b) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400983 if (a->isBuiltin() != b->isBuiltin()) {
984 return a->isBuiltin() < b->isBuiltin();
John Stiles9878d9e2020-09-22 15:40:16 -0400985 }
Brian Osmancc914522021-09-24 18:58:37 +0000986 if (a->fOffset != b->fOffset) {
987 return a->fOffset < b->fOffset;
John Stiles9878d9e2020-09-22 15:40:16 -0400988 }
Ethan Nicholase2c49992020-10-05 11:49:11 -0400989 if (a->name() != b->name()) {
990 return a->name() < b->name();
John Stiles9878d9e2020-09-22 15:40:16 -0400991 }
992 return a->description() < b->description();
993 });
994 for (const FunctionDeclaration* f : intrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400995 this->copyIntrinsicIfNeeded(*f);
996 }
John Stiles607d36b2020-10-19 15:00:01 -0400997
Brian Osman0006ad02020-11-18 15:38:39 -0500998 fSharedElements->push_back(found);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400999 }
1000}
1001
Brian Osmancc914522021-09-24 18:58:37 +00001002std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001003 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -04001004 ExpressionArray arguments) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001005 if (function.isBuiltin()) {
John Stiles12eb0ee2021-06-01 14:14:50 -04001006 if (function.intrinsicKind() == k_dFdy_IntrinsicKind) {
Brian Salomond8d85b92021-07-07 09:41:17 -04001007 fInputs.fUseFlipRTUniform = true;
John Stiles12eb0ee2021-06-01 14:14:50 -04001008 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001009 if (!fIsBuiltinCode && fIntrinsics) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001010 this->copyIntrinsicIfNeeded(function);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001011 }
1012 }
John Stilesea9ab822020-08-31 09:55:04 -04001013
Brian Osmancc914522021-09-24 18:58:37 +00001014 return FunctionCall::Convert(fContext, offset, function, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001015}
1016
1017/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001018 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
Brian Osman0acb5b52020-09-02 13:45:47 -04001019 * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if
1020 * the call is not valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001021 */
Brian Osman0acb5b52020-09-02 13:45:47 -04001022CoercionCost IRGenerator::callCost(const FunctionDeclaration& function,
Brian Osman3099f792021-09-01 13:12:16 -04001023 const ExpressionArray& arguments) const {
John Stilesefde90d2021-08-12 23:06:24 -04001024 if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) {
1025 return CoercionCost::Impossible();
1026 }
Ethan Nicholased84b732020-10-08 11:45:44 -04001027 if (function.parameters().size() != arguments.size()) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001028 return CoercionCost::Impossible();
ethannicholasb3058bd2016-07-01 08:22:01 -07001029 }
John Stilesfa889112020-10-12 19:03:43 -04001030 FunctionDeclaration::ParamTypes types;
ethannicholas471e8942016-10-28 09:02:46 -07001031 const Type* ignored;
1032 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001033 return CoercionCost::Impossible();
ethannicholas471e8942016-10-28 09:02:46 -07001034 }
Brian Osman0acb5b52020-09-02 13:45:47 -04001035 CoercionCost total = CoercionCost::Free();
ethannicholasb3058bd2016-07-01 08:22:01 -07001036 for (size_t i = 0; i < arguments.size(); i++) {
Brian Osman0acb5b52020-09-02 13:45:47 -04001037 total = total + arguments[i]->coercionCost(*types[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001038 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001039 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001040}
1041
Brian Osman3099f792021-09-01 13:12:16 -04001042const FunctionDeclaration* IRGenerator::findBestFunctionForCall(
1043 const std::vector<const FunctionDeclaration*>& functions,
1044 const ExpressionArray& arguments) const {
1045 if (functions.size() == 1) {
1046 return functions.front();
1047 }
1048 CoercionCost bestCost = CoercionCost::Impossible();
1049 const FunctionDeclaration* best = nullptr;
1050 for (const auto& f : functions) {
1051 CoercionCost cost = this->callCost(*f, arguments);
1052 if (cost < bestCost) {
1053 bestCost = cost;
1054 best = f;
1055 }
1056 }
1057 return best;
1058}
1059
Brian Osmancc914522021-09-24 18:58:37 +00001060std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001061 std::unique_ptr<Expression> functionValue,
John Stiles8e3b6be2020-10-13 11:14:08 -04001062 ExpressionArray arguments) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001063 switch (functionValue->kind()) {
1064 case Expression::Kind::kTypeReference:
John Stiles23521a82021-03-02 17:02:51 -05001065 return Constructor::Convert(fContext,
Brian Osmancc914522021-09-24 18:58:37 +00001066 offset,
John Stiles23521a82021-03-02 17:02:51 -05001067 functionValue->as<TypeReference>().value(),
1068 std::move(arguments));
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001069 case Expression::Kind::kExternalFunctionReference: {
1070 const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function();
1071 int count = f.callParameterCount();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001072 if (count != (int) arguments.size()) {
Brian Osmancc914522021-09-24 18:58:37 +00001073 this->errorReporter().error(offset, "external function expected " +
1074 to_string(count) + " arguments, but found " +
1075 to_string((int)arguments.size()));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001076 return nullptr;
1077 }
1078 static constexpr int PARAMETER_MAX = 16;
1079 SkASSERT(count < PARAMETER_MAX);
1080 const Type* types[PARAMETER_MAX];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001081 f.getCallParameterTypes(types);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001082 for (int i = 0; i < count; ++i) {
John Stiles63861992021-09-22 10:22:16 -04001083 arguments[i] = types[i]->coerceExpression(std::move(arguments[i]), fContext);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001084 if (!arguments[i]) {
1085 return nullptr;
1086 }
1087 }
Brian Osmancc914522021-09-24 18:58:37 +00001088 return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001089 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001090 case Expression::Kind::kFunctionReference: {
John Stilesce591b72020-08-27 11:47:30 -04001091 const FunctionReference& ref = functionValue->as<FunctionReference>();
Ethan Nicholas5194a702020-10-12 11:12:12 -04001092 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
Brian Osman3099f792021-09-01 13:12:16 -04001093 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1094 if (best) {
Brian Osmancc914522021-09-24 18:58:37 +00001095 return this->call(offset, *best, std::move(arguments));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001096 }
Brian Osman3099f792021-09-01 13:12:16 -04001097 String msg = "no match for " + functions[0]->name() + "(";
1098 String separator;
1099 for (size_t i = 0; i < arguments.size(); i++) {
1100 msg += separator;
1101 separator = ", ";
1102 msg += arguments[i]->type().displayName();
1103 }
1104 msg += ")";
Brian Osmancc914522021-09-24 18:58:37 +00001105 this->errorReporter().error(offset, msg);
Brian Osman3099f792021-09-01 13:12:16 -04001106 return nullptr;
1107 }
1108 case Expression::Kind::kMethodReference: {
1109 MethodReference& ref = functionValue->as<MethodReference>();
1110 arguments.push_back(std::move(ref.self()));
1111
1112 const std::vector<const FunctionDeclaration*>& functions = ref.functions();
1113 const FunctionDeclaration* best = this->findBestFunctionForCall(functions, arguments);
1114 if (best) {
Brian Osmancc914522021-09-24 18:58:37 +00001115 return this->call(offset, *best, std::move(arguments));
Brian Osman3099f792021-09-01 13:12:16 -04001116 }
1117 String msg = "no match for " + arguments.back()->type().displayName() +
1118 "::" + functions[0]->name().substr(1) + "(";
1119 String separator;
1120 for (size_t i = 0; i < arguments.size() - 1; i++) {
1121 msg += separator;
1122 separator = ", ";
1123 msg += arguments[i]->type().displayName();
1124 }
1125 msg += ")";
Brian Osmancc914522021-09-24 18:58:37 +00001126 this->errorReporter().error(offset, msg);
Brian Osman3099f792021-09-01 13:12:16 -04001127 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001128 }
Ethan Nicholasace470b2021-08-30 16:15:20 -04001129 case Expression::Kind::kPoison:
1130 return functionValue;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001131 default:
Brian Osmancc914522021-09-24 18:58:37 +00001132 this->errorReporter().error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001133 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001134 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001135}
1136
Ethan Nicholasfc994162019-06-06 10:04:27 -04001137std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
1138 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
1139 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07001140 if (!base) {
1141 return nullptr;
1142 }
John Stiles23521a82021-03-02 17:02:51 -05001143 return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base));
ethannicholasb3058bd2016-07-01 08:22:01 -07001144}
1145
ethannicholasb3058bd2016-07-01 08:22:01 -07001146std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001147 skstd::string_view fields) {
Ethan Nicholas59ff4e22021-07-12 12:28:57 -04001148 return Swizzle::Convert(fContext, std::move(base), fields);
ethannicholasb3058bd2016-07-01 08:22:01 -07001149}
1150
Ethan Nicholasfc994162019-06-06 10:04:27 -04001151std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
1152 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
1153 auto iter = index.begin();
1154 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001155 if (!base) {
1156 return nullptr;
1157 }
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001158 if (iter == index.end()) {
Ethan Nicholas251e6342021-07-20 13:16:19 -04001159 if (base->is<TypeReference>()) {
Brian Osmancc914522021-09-24 18:58:37 +00001160 this->errorReporter().error(index.fOffset, "array must have a size");
Ethan Nicholas251e6342021-07-20 13:16:19 -04001161 } else {
Brian Osmancc914522021-09-24 18:58:37 +00001162 this->errorReporter().error(base->fOffset, "missing index in '[]'");
Ethan Nicholas251e6342021-07-20 13:16:19 -04001163 }
Brian Osman4cf85072021-01-14 14:18:10 -05001164 return nullptr;
Ethan Nicholas4d2bbbb2021-01-12 11:56:23 -05001165 }
1166 std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
1167 if (!converted) {
1168 return nullptr;
1169 }
John Stilesbb8cf582021-08-26 23:34:59 -04001170 return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted));
John Stiles1b27c3d2020-12-07 12:14:55 -05001171}
1172
Ethan Nicholasfc994162019-06-06 10:04:27 -04001173std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
1174 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
1175 auto iter = callNode.begin();
1176 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
1177 if (!base) {
1178 return nullptr;
1179 }
John Stiles8e3b6be2020-10-13 11:14:08 -04001180 ExpressionArray arguments;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001181 for (; iter != callNode.end(); ++iter) {
1182 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
1183 if (!converted) {
1184 return nullptr;
1185 }
1186 arguments.push_back(std::move(converted));
1187 }
Brian Osmancc914522021-09-24 18:58:37 +00001188 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
Ethan Nicholasfc994162019-06-06 10:04:27 -04001189}
1190
1191std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
1192 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
1193 if (!base) {
1194 return nullptr;
1195 }
Ethan Nicholas962dec42021-06-10 13:06:39 -04001196 const skstd::string_view& field = fieldNode.getStringView();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001197 const Type& baseType = base->type();
Brian Osman3099f792021-09-01 13:12:16 -04001198 if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct() || baseType.isEffectChild()) {
1199 return FieldAccess::Convert(fContext, *fSymbolTable, std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001200 }
John Stiles06d600f2021-03-08 09:18:21 -05001201 return this->convertSwizzle(std::move(base), field);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001202}
1203
1204std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
Ethan Nicholas9ead3df2021-01-06 12:10:48 -05001205 SkASSERT(expression.fKind == ASTNode::Kind::kPostfix);
Ethan Nicholasfc994162019-06-06 10:04:27 -04001206 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
1207 if (!base) {
1208 return nullptr;
1209 }
John Stiles23521a82021-03-02 17:02:51 -05001210 return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator());
ethannicholasb3058bd2016-07-01 08:22:01 -07001211}
1212
Brian Osman9496fe52020-11-18 14:48:19 -05001213void IRGenerator::findAndDeclareBuiltinVariables() {
1214 class BuiltinVariableScanner : public ProgramVisitor {
Brian Osman8e2ef022020-09-30 13:26:43 -04001215 public:
Brian Osman9496fe52020-11-18 14:48:19 -05001216 BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {}
Brian Osman8e2ef022020-09-30 13:26:43 -04001217
Brian Osman9496fe52020-11-18 14:48:19 -05001218 void addDeclaringElement(const String& name) {
Brian Osman00a8b5b2020-10-02 09:06:04 -04001219 // If this is the *first* time we've seen this builtin, findAndInclude will return
1220 // the corresponding ProgramElement.
Brian Osman9496fe52020-11-18 14:48:19 -05001221 if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001222 SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>());
Brian Osman9496fe52020-11-18 14:48:19 -05001223 fNewElements.push_back(decl);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001224 }
1225 }
1226
John Stiles4d6310a2021-01-26 19:58:22 -05001227 bool visitProgramElement(const ProgramElement& pe) override {
1228 if (pe.is<FunctionDefinition>()) {
1229 const FunctionDefinition& funcDef = pe.as<FunctionDefinition>();
1230 // We synthesize writes to sk_FragColor if main() returns a color, even if it's
1231 // otherwise unreferenced. Check main's return type to see if it's half4.
John Stilese8da4d22021-03-24 09:19:45 -04001232 if (funcDef.declaration().isMain() &&
John Stiles4d6310a2021-01-26 19:58:22 -05001233 funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) {
1234 fPreserveFragColor = true;
1235 }
1236 }
1237 return INHERITED::visitProgramElement(pe);
1238 }
1239
Brian Osman9496fe52020-11-18 14:48:19 -05001240 bool visitExpression(const Expression& e) override {
Ethan Nicholas78686922020-10-08 06:46:27 -04001241 if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) {
Ethan Nicholasd2e09602021-06-10 11:21:59 -04001242 this->addDeclaringElement(String(e.as<VariableReference>().variable()->name()));
Brian Osman8e2ef022020-09-30 13:26:43 -04001243 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001244 return INHERITED::visitExpression(e);
1245 }
1246
1247 IRGenerator* fGenerator;
Brian Osman9496fe52020-11-18 14:48:19 -05001248 std::vector<const ProgramElement*> fNewElements;
John Stiles4d6310a2021-01-26 19:58:22 -05001249 bool fPreserveFragColor = false;
Brian Osman8e2ef022020-09-30 13:26:43 -04001250
Brian Osman9496fe52020-11-18 14:48:19 -05001251 using INHERITED = ProgramVisitor;
Brian Osman8e2ef022020-09-30 13:26:43 -04001252 using INHERITED::visitProgramElement;
1253 };
1254
Brian Osman9496fe52020-11-18 14:48:19 -05001255 BuiltinVariableScanner scanner(this);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001256 SkASSERT(fProgramElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -04001257 for (auto& e : *fProgramElements) {
Brian Osman9496fe52020-11-18 14:48:19 -05001258 scanner.visitProgramElement(*e);
Brian Osman8e2ef022020-09-30 13:26:43 -04001259 }
Brian Osman00a8b5b2020-10-02 09:06:04 -04001260
John Stiles4d6310a2021-01-26 19:58:22 -05001261 if (scanner.fPreserveFragColor) {
1262 // main() returns a half4, so make sure we don't dead-strip sk_FragColor.
John Stilese67bd132021-03-19 18:39:25 -04001263 scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME);
John Stiles4d6310a2021-01-26 19:58:22 -05001264 }
1265
John Stilesd1204642021-02-17 16:30:02 -05001266 switch (this->programKind()) {
John Stilesdbd4e6f2021-02-16 13:29:15 -05001267 case ProgramKind::kFragment:
John Stiles4d6310a2021-01-26 19:58:22 -05001268 // Vulkan requires certain builtin variables be present, even if they're unused. At one
1269 // time, validation errors would result if sk_Clockwise was missing. Now, it's just
1270 // (Adreno) driver bugs that drop or corrupt draws if they're missing.
Brian Osman9496fe52020-11-18 14:48:19 -05001271 scanner.addDeclaringElement("sk_Clockwise");
Brian Osman00a8b5b2020-10-02 09:06:04 -04001272 break;
1273 default:
1274 break;
1275 }
1276
Brian Osman9496fe52020-11-18 14:48:19 -05001277 fSharedElements->insert(
1278 fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end());
Brian Osman8e2ef022020-09-30 13:26:43 -04001279}
1280
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001281void IRGenerator::start(const ParsedModule& base,
1282 bool isBuiltinCode,
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001283 std::vector<std::unique_ptr<ProgramElement>>* elements,
1284 std::vector<const ProgramElement*>* sharedElements) {
Ethan Nicholas624a5292021-04-16 14:54:43 -04001285 fProgramElements = elements;
1286 fSharedElements = sharedElements;
Brian Osman88cda172020-10-09 12:05:16 -04001287 fSymbolTable = base.fSymbols;
1288 fIntrinsics = base.fIntrinsics.get();
1289 if (fIntrinsics) {
1290 fIntrinsics->resetAlreadyIncluded();
1291 }
1292 fIsBuiltinCode = isBuiltinCode;
1293
Brian Salomond8d85b92021-07-07 09:41:17 -04001294 fInputs = {};
Brian Osman88cda172020-10-09 12:05:16 -04001295 fRTAdjust = nullptr;
1296 fRTAdjustInterfaceBlock = nullptr;
Brian Osman02bc5222021-01-28 11:00:20 -05001297 fDefinedStructs.clear();
John Stilesde2b6092021-09-17 10:40:47 -04001298 SymbolTable::Push(&fSymbolTable, fIsBuiltinCode);
Brian Osman88cda172020-10-09 12:05:16 -04001299
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001300 if (this->settings().fExternalFunctions) {
John Stilesaecf8d52021-05-14 12:15:01 -04001301 // Add any external values to the new symbol table, so they're only visible to this Program.
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001302 for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001303 fSymbolTable->addWithoutOwnership(ef.get());
Brian Osman88cda172020-10-09 12:05:16 -04001304 }
1305 }
John Stilesaecf8d52021-05-14 12:15:01 -04001306
John Stilesbb2ef922021-07-26 08:32:07 -04001307 if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
John Stiles325c8082021-05-17 14:49:05 -04001308 // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
John Stiles12eb0ee2021-06-01 14:14:50 -04001309 // non-ES2 types to our symbol table to allow them to be tested.
John Stilesaecf8d52021-05-14 12:15:01 -04001310 fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
1311 fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
1312 fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
1313 fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
1314 fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
1315 fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
1316 fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
1317 fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
1318 fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
1319
1320 fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
1321 fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
1322 fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
1323 fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
1324 fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
1325 fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
1326
1327 fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
1328 fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
1329 fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
1330 fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
1331 fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
1332 fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
John Stiles325c8082021-05-17 14:49:05 -04001333
1334 fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
1335 fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
1336 fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
1337 fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
John Stiles26487162021-08-10 16:03:44 -04001338
1339 fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
1340 fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
1341 fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
1342 fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
1343
1344 fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
1345 fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
1346 fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
1347 fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
John Stilesaecf8d52021-05-14 12:15:01 -04001348 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001349}
Brian Osman88cda172020-10-09 12:05:16 -04001350
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001351IRGenerator::IRBundle IRGenerator::finish() {
Brian Osman9496fe52020-11-18 14:48:19 -05001352 // Variables defined in the pre-includes need their declaring elements added to the program
Brian Osman00a8b5b2020-10-02 09:06:04 -04001353 if (!fIsBuiltinCode && fIntrinsics) {
Brian Osman9496fe52020-11-18 14:48:19 -05001354 this->findAndDeclareBuiltinVariables();
Brian Osman00a8b5b2020-10-02 09:06:04 -04001355 }
Brian Osman8e2ef022020-09-30 13:26:43 -04001356
John Stilesf2872e62021-05-04 11:38:43 -04001357 return IRBundle{std::move(*fProgramElements),
1358 std::move(*fSharedElements),
John Stilesf2872e62021-05-04 11:38:43 -04001359 std::move(fSymbolTable),
1360 fInputs};
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001361}
1362
1363IRGenerator::IRBundle IRGenerator::convertProgram(
1364 const ParsedModule& base,
1365 bool isBuiltinCode,
Ethan Nicholas6823b502021-06-15 11:42:07 -04001366 skstd::string_view text) {
1367 Parser parser(text, *fSymbolTable, this->errorReporter());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001368 fFile = parser.compilationUnit();
1369 if (this->errorReporter().errorCount() == 0) {
1370 SkASSERT(fFile);
1371 for (const auto& decl : fFile->root()) {
1372 switch (decl.fKind) {
1373 case ASTNode::Kind::kVarDeclarations:
1374 this->convertGlobalVarDeclarations(decl);
1375 break;
1376
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001377 case ASTNode::Kind::kFunction:
1378 this->convertFunction(decl);
1379 break;
1380
1381 case ASTNode::Kind::kModifiers: {
1382 std::unique_ptr<ModifiersDeclaration> f =
1383 this->convertModifiersDeclaration(decl);
1384 if (f) {
1385 fProgramElements->push_back(std::move(f));
1386 }
1387 break;
1388 }
1389 case ASTNode::Kind::kInterfaceBlock: {
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001390 std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001391 if (i) {
1392 fProgramElements->push_back(std::move(i));
1393 }
1394 break;
1395 }
1396 case ASTNode::Kind::kExtension: {
Brian Osmancc914522021-09-24 18:58:37 +00001397 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
Ethan Nicholas962dec42021-06-10 13:06:39 -04001398 decl.getStringView());
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001399 if (e) {
1400 fProgramElements->push_back(std::move(e));
1401 }
1402 break;
1403 }
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001404 case ASTNode::Kind::kType: {
1405 std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl);
1406 if (s) {
1407 fProgramElements->push_back(std::move(s));
1408 }
1409 break;
1410 }
1411 default:
John Stilesfbb905e2021-08-25 15:51:50 -04001412 SkDEBUGFAIL("unsupported declaration");
Ethan Nicholas8b3dd342021-03-23 12:32:56 -04001413 break;
1414 }
1415 }
1416 }
1417 return this->finish();
Brian Osman88cda172020-10-09 12:05:16 -04001418}
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001419
John Stilesa6841be2020-08-06 14:11:56 -04001420} // namespace SkSL