blob: 7c551d6d26a8e35a41ecb012670d25ddf1784fb2 [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 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
John Stilesfbd050b2020-08-03 13:21:46 -040012
John Stilesb92641c2020-08-31 18:09:01 -040013#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/sksl/SkSLByteCodeGenerator.h"
15#include "src/sksl/SkSLCFGGenerator.h"
16#include "src/sksl/SkSLCPPCodeGenerator.h"
17#include "src/sksl/SkSLGLSLCodeGenerator.h"
18#include "src/sksl/SkSLHCodeGenerator.h"
19#include "src/sksl/SkSLIRGenerator.h"
20#include "src/sksl/SkSLMetalCodeGenerator.h"
21#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040022#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050024#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLEnum.h"
26#include "src/sksl/ir/SkSLExpression.h"
27#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLFunctionCall.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLModifiersDeclaration.h"
31#include "src/sksl/ir/SkSLNop.h"
32#include "src/sksl/ir/SkSLSymbolTable.h"
33#include "src/sksl/ir/SkSLTernaryExpression.h"
34#include "src/sksl/ir/SkSLUnresolvedFunction.h"
35#include "src/sksl/ir/SkSLVarDeclarations.h"
John Stilese6150002020-10-05 12:03:53 -040036#include "src/utils/SkBitSet.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070037
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040038#include <fstream>
39
Ethan Nicholasa11035b2019-11-26 16:27:47 -050040#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
41#include "include/gpu/GrContextOptions.h"
42#include "src/gpu/GrShaderCaps.h"
43#endif
44
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040045#ifdef SK_ENABLE_SPIRV_VALIDATION
46#include "spirv-tools/libspirv.hpp"
47#endif
48
Brian Osman3d87e9f2020-10-08 11:50:22 -040049#if defined(SKSL_STANDALONE)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050
Brian Osman3d87e9f2020-10-08 11:50:22 -040051// In standalone mode, we load the textual sksl source files. GN generates or copies these files
52// to the skslc executable directory. The "data" in this mode is just the filename.
53#define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl")
54
55#else
56
57// At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair.
Ethan Nicholasc18bb512020-07-28 14:46:53 -040058#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
59#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
60#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
61#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
62#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
63#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
64#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
65
Brian Osman3d87e9f2020-10-08 11:50:22 -040066#define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\
67 SKSL_INCLUDE_sksl_##name##_LENGTH)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068
69#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040070
ethannicholasb3058bd2016-07-01 08:22:01 -070071namespace SkSL {
72
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040073Compiler::Compiler(Flags flags)
Brian Osman00a8b5b2020-10-02 09:06:04 -040074: fFlags(flags)
John Stiles810c8cf2020-08-26 19:46:27 -040075, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040076, fErrorCount(0) {
Brian Osmaneac49832020-09-18 11:49:22 -040077 fRootSymbolTable = std::make_shared<SymbolTable>(this);
John Stiles941fc712020-09-19 12:47:10 +000078 fIRGenerator =
79 std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this);
John Stilesb8cc6652020-10-08 09:12:07 -040080 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070081 ADD_TYPE(Void);
82 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040083 ADD_TYPE(Float2);
84 ADD_TYPE(Float3);
85 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040086 ADD_TYPE(Half);
87 ADD_TYPE(Half2);
88 ADD_TYPE(Half3);
89 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -070090 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040091 ADD_TYPE(Int2);
92 ADD_TYPE(Int3);
93 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -070094 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040095 ADD_TYPE(UInt2);
96 ADD_TYPE(UInt3);
97 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040098 ADD_TYPE(Short);
99 ADD_TYPE(Short2);
100 ADD_TYPE(Short3);
101 ADD_TYPE(Short4);
102 ADD_TYPE(UShort);
103 ADD_TYPE(UShort2);
104 ADD_TYPE(UShort3);
105 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400106 ADD_TYPE(Byte);
107 ADD_TYPE(Byte2);
108 ADD_TYPE(Byte3);
109 ADD_TYPE(Byte4);
110 ADD_TYPE(UByte);
111 ADD_TYPE(UByte2);
112 ADD_TYPE(UByte3);
113 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700114 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400115 ADD_TYPE(Bool2);
116 ADD_TYPE(Bool3);
117 ADD_TYPE(Bool4);
118 ADD_TYPE(Float2x2);
119 ADD_TYPE(Float2x3);
120 ADD_TYPE(Float2x4);
121 ADD_TYPE(Float3x2);
122 ADD_TYPE(Float3x3);
123 ADD_TYPE(Float3x4);
124 ADD_TYPE(Float4x2);
125 ADD_TYPE(Float4x3);
126 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400127 ADD_TYPE(Half2x2);
128 ADD_TYPE(Half2x3);
129 ADD_TYPE(Half2x4);
130 ADD_TYPE(Half3x2);
131 ADD_TYPE(Half3x3);
132 ADD_TYPE(Half3x4);
133 ADD_TYPE(Half4x2);
134 ADD_TYPE(Half4x3);
135 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400137 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700138 ADD_TYPE(GenIType);
139 ADD_TYPE(GenUType);
140 ADD_TYPE(GenBType);
141 ADD_TYPE(Mat);
142 ADD_TYPE(Vec);
143 ADD_TYPE(GVec);
144 ADD_TYPE(GVec2);
145 ADD_TYPE(GVec3);
146 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400147 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 ADD_TYPE(IVec);
149 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400150 ADD_TYPE(SVec);
151 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400152 ADD_TYPE(ByteVec);
153 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 ADD_TYPE(BVec);
155
156 ADD_TYPE(Sampler1D);
157 ADD_TYPE(Sampler2D);
158 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700159 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 ADD_TYPE(SamplerCube);
161 ADD_TYPE(Sampler2DRect);
162 ADD_TYPE(Sampler1DArray);
163 ADD_TYPE(Sampler2DArray);
164 ADD_TYPE(SamplerCubeArray);
165 ADD_TYPE(SamplerBuffer);
166 ADD_TYPE(Sampler2DMS);
167 ADD_TYPE(Sampler2DMSArray);
168
Brian Salomonbf7b6202016-11-11 16:08:03 -0500169 ADD_TYPE(ISampler2D);
170
Brian Salomon2a51de82016-11-16 12:06:01 -0500171 ADD_TYPE(Image2D);
172 ADD_TYPE(IImage2D);
173
Greg Daniel64773e62016-11-22 09:44:03 -0500174 ADD_TYPE(SubpassInput);
175 ADD_TYPE(SubpassInputMS);
176
ethannicholasb3058bd2016-07-01 08:22:01 -0700177 ADD_TYPE(GSampler1D);
178 ADD_TYPE(GSampler2D);
179 ADD_TYPE(GSampler3D);
180 ADD_TYPE(GSamplerCube);
181 ADD_TYPE(GSampler2DRect);
182 ADD_TYPE(GSampler1DArray);
183 ADD_TYPE(GSampler2DArray);
184 ADD_TYPE(GSamplerCubeArray);
185 ADD_TYPE(GSamplerBuffer);
186 ADD_TYPE(GSampler2DMS);
187 ADD_TYPE(GSampler2DMSArray);
188
189 ADD_TYPE(Sampler1DShadow);
190 ADD_TYPE(Sampler2DShadow);
191 ADD_TYPE(SamplerCubeShadow);
192 ADD_TYPE(Sampler2DRectShadow);
193 ADD_TYPE(Sampler1DArrayShadow);
194 ADD_TYPE(Sampler2DArrayShadow);
195 ADD_TYPE(SamplerCubeArrayShadow);
196 ADD_TYPE(GSampler2DArrayShadow);
197 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400198 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400199 ADD_TYPE(Sampler);
200 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700201
Brian Osman28590d52020-03-23 16:59:08 -0400202 StringFragment fpAliasName("shader");
John Stiles49a547f2020-10-06 16:14:37 -0400203 fRootSymbolTable->addAlias(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400204
Brian Osman3887a012020-09-30 13:22:27 -0400205 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
206 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700207 StringFragment skCapsName("sk_Caps");
John Stilesb8cc6652020-10-08 09:12:07 -0400208 fRootSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400209 fIRGenerator->fModifiers->handle(Modifiers()),
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400210 skCapsName,
211 fContext->fSkCaps_Type.get(),
212 /*builtin=*/false,
213 Variable::Storage::kGlobal));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500214
Brian Osman3d87e9f2020-10-08 11:50:22 -0400215 fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr};
John Stilesbc0c29e2020-09-28 13:13:40 -0400216
Brian Osman3d87e9f2020-10-08 11:50:22 -0400217 fGPUModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(gpu), fRootModule);
218 fVertexModule = this->parseModule(Program::kVertex_Kind, MODULE_DATA(vert), fGPUModule);
219 fFragmentModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(frag), fGPUModule);
ethannicholasb3058bd2016-07-01 08:22:01 -0700220}
221
John Stiles656427a2020-08-27 15:26:26 -0400222Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400224void Compiler::loadGeometryIntrinsics() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400225 if (!fGeometryModule.fSymbols) {
226 fGeometryModule = this->parseModule(Program::kGeometry_Kind, MODULE_DATA(geom), fGPUModule);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400227 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228}
229
Brian Osman8e2ef022020-09-30 13:26:43 -0400230void Compiler::loadFPIntrinsics() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400231 if (!fFPModule.fSymbols) {
232 fFPModule =
233 this->parseModule(Program::kFragmentProcessor_Kind, MODULE_DATA(fp), fGPUModule);
Brian Osman8e2ef022020-09-30 13:26:43 -0400234 }
Brian Osman8e2ef022020-09-30 13:26:43 -0400235}
236
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400237void Compiler::loadPipelineIntrinsics() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400238 if (!fPipelineModule.fSymbols) {
239 fPipelineModule =
240 this->parseModule(Program::kPipelineStage_Kind, MODULE_DATA(pipeline), fGPUModule);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242}
243
244void Compiler::loadInterpreterIntrinsics() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400245 if (!fInterpreterModule.fSymbols) {
246 fInterpreterModule =
247 this->parseModule(Program::kGeneric_Kind, MODULE_DATA(interp), fRootModule);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400248 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249}
250
Brian Osman3d87e9f2020-10-08 11:50:22 -0400251LoadedModule Compiler::loadModule(Program::Kind kind,
252 ModuleData data,
253 std::shared_ptr<SymbolTable> base) {
254 LoadedModule module;
255 if (!base) {
256 base = fRootSymbolTable;
257 }
258
259#if defined(SKSL_STANDALONE)
260 SkASSERT(data.fPath);
261 std::ifstream in(data.fPath);
Brian Osmane498b3c2020-09-23 14:42:11 -0400262 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
263 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400264 if (in.rdstate()) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400265 printf("error reading %s\n", data.fPath);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400266 abort();
267 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400268 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400269 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400270 Program::Settings settings;
John Stiles881a10c2020-09-19 10:13:24 -0400271 SkASSERT(fIRGenerator->fCanInline);
272 fIRGenerator->fCanInline = false;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400273 fIRGenerator->start(&settings, {base, /*fIntrinsics=*/nullptr}, /*builtin=*/true);
274 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &module.fElements);
John Stiles881a10c2020-09-19 10:13:24 -0400275 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400276 if (this->fErrorCount) {
277 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400278 SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400279 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400280 module.fSymbols = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500281 fSource = nullptr;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400282 fModifiers.push_back(fIRGenerator->releaseModifiers());
Brian Osmane498b3c2020-09-23 14:42:11 -0400283 fIRGenerator->finish();
Brian Osman3d87e9f2020-10-08 11:50:22 -0400284#else
285 SkASSERT(data.fData && (data.fSize != 0));
286 Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this,
287 data.fData, data.fSize);
288 module = { rehydrator.symbolTable(), rehydrator.elements() };
289 fModifiers.push_back(fIRGenerator->releaseModifiers());
290#endif
291
292 return module;
293}
294
295ParsedModule Compiler::parseModule(Program::Kind kind, ModuleData data, const ParsedModule& base) {
296 auto [symbols, elements] = this->loadModule(kind, data, base.fSymbols);
297
298 // For modules that just declare (but don't define) intrinsic functions, there will be no new
299 // program elements. In that case, we can share our parent's intrinsic map:
300 if (elements.empty()) {
301 return {symbols, base.fIntrinsics};
302 }
303
304 auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get());
305
306 // Now, transfer all of the program elements to an intrinsic map. This maps certain types of
307 // global objects to the declaring ProgramElement.
308 for (std::unique_ptr<ProgramElement>& element : elements) {
309 switch (element->kind()) {
310 case ProgramElement::Kind::kFunction: {
311 const FunctionDefinition& f = element->as<FunctionDefinition>();
312 SkASSERT(f.fDeclaration.isBuiltin());
313 // Call counts are used to track dead-stripping and inlinability within the program
314 // being compiled, and should start at zero for a new program. Zero out any call
315 // counts internal to the include data. (If we actually use calls from inside the
316 // intrinsics, we will clone them into the program with new call counts.)
317 f.fDeclaration.callCount() = 0;
318 intrinsics->insertOrDie(f.fDeclaration.description(), std::move(element));
319 break;
320 }
321 case ProgramElement::Kind::kEnum: {
322 const Enum& e = element->as<Enum>();
323 SkASSERT(e.isBuiltin());
324 intrinsics->insertOrDie(e.typeName(), std::move(element));
325 break;
326 }
327 case ProgramElement::Kind::kGlobalVar: {
328 const Variable* var = element->as<GlobalVarDeclaration>().fDecl->fVar;
329 SkASSERT(var->isBuiltin());
330 intrinsics->insertOrDie(var->name(), std::move(element));
331 break;
332 }
333 case ProgramElement::Kind::kInterfaceBlock: {
334 const Variable* var = element->as<InterfaceBlock>().fVariable;
335 SkASSERT(var->isBuiltin());
336 intrinsics->insertOrDie(var->name(), std::move(element));
337 break;
338 }
339 default:
340 printf("Unsupported element: %s\n", element->description().c_str());
341 SkASSERT(false);
342 break;
343 }
344 }
345
346 return {symbols, std::move(intrinsics)};
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400347}
348
ethannicholas22f939e2016-10-13 13:25:34 -0700349// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500350void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
351 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400352 switch (lvalue->kind()) {
353 case Expression::Kind::kVariableReference: {
Ethan Nicholas78686922020-10-08 06:46:27 -0400354 const Variable& var = *lvalue->as<VariableReference>().variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400355 if (var.storage() == Variable::Storage::kLocal) {
John Stiles796cdb72020-10-08 12:06:53 -0400356 definitions->set(&var, expr);
ethannicholas22f939e2016-10-13 13:25:34 -0700357 }
358 break;
359 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400360 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700361 // We consider the variable written to as long as at least some of its components have
362 // been written to. This will lead to some false negatives (we won't catch it if you
363 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400364 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
365 // but since we pass foo as a whole it is flagged as an error) unless we perform a much
ethannicholas22f939e2016-10-13 13:25:34 -0700366 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400367 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400368 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700369 definitions);
370 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400371 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700372 // see comments in Swizzle
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400373 this->addDefinition(lvalue->as<IndexExpression>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400374 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700375 definitions);
376 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400377 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700378 // see comments in Swizzle
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400379 this->addDefinition(lvalue->as<FieldAccess>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400380 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700381 definitions);
382 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400383 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500384 // To simplify analysis, we just pretend that we write to both sides of the ternary.
385 // This allows for false positives (meaning we fail to detect that a variable might not
386 // have been assigned), but is preferable to false negatives.
Ethan Nicholasdd218162020-10-08 05:48:01 -0400387 this->addDefinition(lvalue->as<TernaryExpression>().ifTrue().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400388 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500389 definitions);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400390 this->addDefinition(lvalue->as<TernaryExpression>().ifFalse().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400391 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500392 definitions);
393 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400394 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400395 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700396 default:
397 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400398 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700399 }
400}
401
402// add local variables defined by this node to the set
John Stiles796cdb72020-10-08 12:06:53 -0400403void Compiler::addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400404 if (node.isExpression()) {
405 Expression* expr = node.expression()->get();
406 switch (expr->kind()) {
407 case Expression::Kind::kBinary: {
408 BinaryExpression* b = &expr->as<BinaryExpression>();
409 if (b->getOperator() == Token::Kind::TK_EQ) {
410 this->addDefinition(&b->left(), &b->rightPointer(), definitions);
411 } else if (Compiler::IsAssignment(b->getOperator())) {
412 this->addDefinition(
413 &b->left(),
414 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
415 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500416
ethannicholas22f939e2016-10-13 13:25:34 -0700417 }
John Stiles70025e52020-09-28 16:08:58 -0400418 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700419 }
John Stiles70025e52020-09-28 16:08:58 -0400420 case Expression::Kind::kFunctionCall: {
421 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholased84b732020-10-08 11:45:44 -0400422 const std::vector<Variable*>& parameters = c.function().parameters();
423 for (size_t i = 0; i < parameters.size(); ++i) {
424 if (parameters[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
John Stiles70025e52020-09-28 16:08:58 -0400425 this->addDefinition(
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400426 c.arguments()[i].get(),
John Stiles70025e52020-09-28 16:08:58 -0400427 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
428 definitions);
429 }
430 }
431 break;
432 }
433 case Expression::Kind::kPrefix: {
434 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400435 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
436 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400437 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400438 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400439 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
440 definitions);
441 }
442 break;
443 }
444 case Expression::Kind::kPostfix: {
445 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400446 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
447 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400448 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400449 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400450 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
451 definitions);
452 }
453 break;
454 }
455 case Expression::Kind::kVariableReference: {
456 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400457 if (v->refKind() != VariableReference::RefKind::kRead) {
John Stiles70025e52020-09-28 16:08:58 -0400458 this->addDefinition(
459 v,
460 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
461 definitions);
462 }
463 break;
464 }
465 default:
466 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700467 }
John Stiles70025e52020-09-28 16:08:58 -0400468 } else if (node.isStatement()) {
469 Statement* stmt = node.statement()->get();
470 if (stmt->is<VarDeclaration>()) {
471 VarDeclaration& vd = stmt->as<VarDeclaration>();
472 if (vd.fValue) {
John Stiles796cdb72020-10-08 12:06:53 -0400473 definitions->set(vd.fVar, &vd.fValue);
ethannicholas22f939e2016-10-13 13:25:34 -0700474 }
ethannicholas22f939e2016-10-13 13:25:34 -0700475 }
476 }
477}
478
John Stilese6150002020-10-05 12:03:53 -0400479void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) {
ethannicholas22f939e2016-10-13 13:25:34 -0700480 BasicBlock& block = cfg->fBlocks[blockId];
481
482 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500483 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700484 for (const BasicBlock::Node& n : block.fNodes) {
485 this->addDefinitions(n, &after);
486 }
487
488 // propagate definitions to exits
489 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400490 if (exitId == blockId) {
491 continue;
492 }
ethannicholas22f939e2016-10-13 13:25:34 -0700493 BasicBlock& exit = cfg->fBlocks[exitId];
John Stiles796cdb72020-10-08 12:06:53 -0400494 after.foreach([&](const Variable* var, std::unique_ptr<Expression>** e1Ptr) {
495 std::unique_ptr<Expression>* e1 = *e1Ptr;
496 std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var);
497 if (!exitDef) {
John Stilese6150002020-10-05 12:03:53 -0400498 // exit has no definition for it, just copy it and reprocess exit block
499 processedSet->reset(exitId);
John Stiles796cdb72020-10-08 12:06:53 -0400500 exit.fBefore[var] = e1;
ethannicholas22f939e2016-10-13 13:25:34 -0700501 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500502 // exit has a (possibly different) value already defined
John Stiles796cdb72020-10-08 12:06:53 -0400503 std::unique_ptr<Expression>* e2 = *exitDef;
ethannicholas22f939e2016-10-13 13:25:34 -0700504 if (e1 != e2) {
John Stilese6150002020-10-05 12:03:53 -0400505 // definition has changed, merge and reprocess the exit block
506 processedSet->reset(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500507 if (e1 && e2) {
John Stiles796cdb72020-10-08 12:06:53 -0400508 *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500509 } else {
John Stiles796cdb72020-10-08 12:06:53 -0400510 *exitDef = nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500511 }
ethannicholas22f939e2016-10-13 13:25:34 -0700512 }
513 }
John Stiles796cdb72020-10-08 12:06:53 -0400514 });
ethannicholas22f939e2016-10-13 13:25:34 -0700515 }
516}
517
518// returns a map which maps all local variables in the function to null, indicating that their value
519// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500520static DefinitionMap compute_start_state(const CFG& cfg) {
521 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400522 for (const auto& block : cfg.fBlocks) {
523 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400524 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400525 const Statement* s = node.statement()->get();
Brian Osmanc0213602020-10-06 14:43:32 -0400526 if (s->is<VarDeclaration>()) {
527 result[s->as<VarDeclaration>().fVar] = nullptr;
ethannicholas22f939e2016-10-13 13:25:34 -0700528 }
529 }
530 }
531 }
532 return result;
533}
534
Ethan Nicholascb670962017-04-20 19:31:52 -0400535/**
536 * Returns true if assigning to this lvalue has no effect.
537 */
538static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400539 switch (lvalue.kind()) {
540 case Expression::Kind::kVariableReference:
Ethan Nicholas78686922020-10-08 06:46:27 -0400541 return lvalue.as<VariableReference>().variable()->dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400542 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400543 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400544 case Expression::Kind::kFieldAccess:
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400545 return is_dead(*lvalue.as<FieldAccess>().base());
Ethan Nicholase6592142020-09-08 10:22:09 -0400546 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400547 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400548 return is_dead(*idx.base()) &&
549 !idx.index()->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400550 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400551 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400552 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400553 return !t.test()->hasSideEffects() && is_dead(*t.ifTrue()) && is_dead(*t.ifFalse());
Ethan Nicholasa583b812018-01-18 13:32:11 -0500554 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400555 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400556 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400557 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500558#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400559 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500560#endif
561 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400562 }
563}
ethannicholas22f939e2016-10-13 13:25:34 -0700564
Ethan Nicholascb670962017-04-20 19:31:52 -0400565/**
566 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
567 * to a dead target and lack of side effects on the left hand side.
568 */
569static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400570 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400571 return false;
572 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400573 return is_dead(b.left());
Ethan Nicholascb670962017-04-20 19:31:52 -0400574}
575
576void Compiler::computeDataFlow(CFG* cfg) {
577 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
John Stilese6150002020-10-05 12:03:53 -0400578
579 // We set bits in the "processed" set after a block has been scanned.
580 SkBitSet processedSet(cfg->fBlocks.size());
581 while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) {
582 processedSet.set(*blockId);
583 this->scanCFG(cfg, *blockId, &processedSet);
ethannicholas22f939e2016-10-13 13:25:34 -0700584 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400585}
586
587/**
588 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
589 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
590 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
591 * need to be regenerated).
592 */
John Stilesafbf8992020-08-18 10:08:21 -0400593static bool try_replace_expression(BasicBlock* b,
594 std::vector<BasicBlock::Node>::iterator* iter,
595 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 std::unique_ptr<Expression>* target = (*iter)->expression();
597 if (!b->tryRemoveExpression(iter)) {
598 *target = std::move(*newExpression);
599 return false;
600 }
601 *target = std::move(*newExpression);
602 return b->tryInsertExpression(iter, target);
603}
604
605/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400606 * Returns true if the expression is a constant numeric literal with the specified value, or a
607 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400608 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400609template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400610static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400611 switch (expr.kind()) {
612 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400613 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400614
Ethan Nicholase6592142020-09-08 10:22:09 -0400615 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400616 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400617
Ethan Nicholase6592142020-09-08 10:22:09 -0400618 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400619 const Constructor& constructor = expr.as<Constructor>();
620 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400621 const Type& constructorType = constructor.type();
622 bool isFloat = constructorType.columns() > 1
623 ? constructorType.componentType().isFloat()
624 : constructorType.isFloat();
625 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400626 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400627 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400628 if (isFloat) {
629 if (constructor.getFVecComponent(i) != value) {
630 return false;
631 }
632 } else {
633 if (constructor.getIVecComponent(i) != value) {
634 return false;
635 }
636 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400637 }
John Stiles9d944232020-08-19 09:56:49 -0400638 return true;
639
Ethan Nicholase6592142020-09-08 10:22:09 -0400640 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400641 SkASSERT(constructor.arguments().size() == 1);
642 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400643
644 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400645 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400646 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400647 }
648 return false;
649 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400650 default:
651 return false;
652 }
653}
654
655/**
656 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
657 * and CFG structures).
658 */
John Stilesafbf8992020-08-18 10:08:21 -0400659static void delete_left(BasicBlock* b,
660 std::vector<BasicBlock::Node>::iterator* iter,
661 bool* outUpdated,
662 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400663 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400664 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400665 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400666 Expression& left = bin.left();
667 std::unique_ptr<Expression>& rightPointer = bin.rightPointer();
668 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400669 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400670 if (bin.getOperator() == Token::Kind::TK_EQ) {
671 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400672 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400673 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400674 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400675 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400676 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400677 *outNeedsRescan = true;
678 return;
679 }
680 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400681 *outNeedsRescan = true;
682 return;
683 }
684 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400685 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400686 *outNeedsRescan = true;
687 return;
688 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400689 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400690 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400691}
692
693/**
694 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
695 * CFG structures).
696 */
John Stilesafbf8992020-08-18 10:08:21 -0400697static void delete_right(BasicBlock* b,
698 std::vector<BasicBlock::Node>::iterator* iter,
699 bool* outUpdated,
700 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400701 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400702 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400703 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400704 std::unique_ptr<Expression>& leftPointer = bin.leftPointer();
705 Expression& right = bin.right();
706 SkASSERT(!right.hasSideEffects());
707 if (!b->tryRemoveExpressionBefore(iter, &right)) {
708 *target = std::move(leftPointer);
Ethan Nicholascb670962017-04-20 19:31:52 -0400709 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400710 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400711 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400712 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400713 if (*iter == b->fNodes.begin()) {
714 *outNeedsRescan = true;
715 return;
716 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400717 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400718 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400719 *outNeedsRescan = true;
720 return;
721 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400722 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400723 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400724}
725
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400726/**
727 * Constructs the specified type using a single argument.
728 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400729static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400730 std::vector<std::unique_ptr<Expression>> args;
731 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400732 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400733 return result;
734}
735
736/**
737 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
738 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
739 */
740static void vectorize(BasicBlock* b,
741 std::vector<BasicBlock::Node>::iterator* iter,
742 const Type& type,
743 std::unique_ptr<Expression>* otherExpression,
744 bool* outUpdated,
745 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400746 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
747 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400748 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400749 *outUpdated = true;
750 std::unique_ptr<Expression>* target = (*iter)->expression();
751 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400752 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400753 *outNeedsRescan = true;
754 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400755 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400756 if (!b->tryInsertExpression(iter, target)) {
757 *outNeedsRescan = true;
758 }
759 }
760}
761
762/**
763 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
764 * left to yield vec<n>(x).
765 */
766static void vectorize_left(BasicBlock* b,
767 std::vector<BasicBlock::Node>::iterator* iter,
768 bool* outUpdated,
769 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400770 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400771 vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400772}
773
774/**
775 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
776 * right to yield vec<n>(y).
777 */
778static void vectorize_right(BasicBlock* b,
779 std::vector<BasicBlock::Node>::iterator* iter,
780 bool* outUpdated,
781 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400782 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400783 vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400784}
785
786// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400787static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400788 switch (expr.kind()) {
789 case Expression::Kind::kVariableReference: {
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400790 expr.as<VariableReference>().setRefKind(VariableReference::RefKind::kRead);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400791 break;
792 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400793 case Expression::Kind::kFieldAccess:
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400794 clear_write(*expr.as<FieldAccess>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400795 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400796 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400797 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400798 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400799 case Expression::Kind::kIndex:
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400800 clear_write(*expr.as<IndexExpression>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400801 break;
802 default:
803 ABORT("shouldn't be writing to this kind of expression\n");
804 break;
805 }
806}
807
Ethan Nicholascb670962017-04-20 19:31:52 -0400808void Compiler::simplifyExpression(DefinitionMap& definitions,
809 BasicBlock& b,
810 std::vector<BasicBlock::Node>::iterator* iter,
811 std::unordered_set<const Variable*>* undefinedVariables,
812 bool* outUpdated,
813 bool* outNeedsRescan) {
814 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400815 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400816 if ((*iter)->fConstantPropagation) {
817 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
818 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400819 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400820 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400821 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400822 if (!try_replace_expression(&b, iter, &optimized)) {
823 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400824 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400825 }
John Stiles70025e52020-09-28 16:08:58 -0400826 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400827 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400828 }
829 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400830 switch (expr->kind()) {
831 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400832 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400833 const Variable* var = ref.variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400834 if (ref.refKind() != VariableReference::RefKind::kWrite &&
835 ref.refKind() != VariableReference::RefKind::kPointer &&
836 var->storage() == Variable::Storage::kLocal && !definitions[var] &&
Brian Osman79457ef2020-09-24 15:01:27 -0400837 (*undefinedVariables).find(var) == (*undefinedVariables).end()) {
838 (*undefinedVariables).insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000839 this->error(expr->fOffset,
Ethan Nicholase2c49992020-10-05 11:49:11 -0400840 "'" + var->name() + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400841 }
842 break;
843 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400844 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400845 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400846 if (t->test()->is<BoolLiteral>()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 // ternary has a constant test, replace it with either the true or
848 // false branch
Ethan Nicholasdd218162020-10-08 05:48:01 -0400849 if (t->test()->as<BoolLiteral>().value()) {
850 (*iter)->setExpression(std::move(t->ifTrue()));
Ethan Nicholascb670962017-04-20 19:31:52 -0400851 } else {
Ethan Nicholasdd218162020-10-08 05:48:01 -0400852 (*iter)->setExpression(std::move(t->ifFalse()));
Ethan Nicholascb670962017-04-20 19:31:52 -0400853 }
854 *outUpdated = true;
855 *outNeedsRescan = true;
856 }
857 break;
858 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400859 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400860 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400861 if (dead_assignment(*bin)) {
862 delete_left(&b, iter, outUpdated, outNeedsRescan);
863 break;
864 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400865 Expression& left = bin->left();
866 Expression& right = bin->right();
867 const Type& leftType = left.type();
868 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400869 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400870 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
871 (leftType.typeKind() != Type::TypeKind::kVector)) ||
872 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
873 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400874 break;
875 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400876 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400877 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400878 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400879 if (leftType.typeKind() == Type::TypeKind::kVector &&
880 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400881 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400882 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
883 } else {
884 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400885 // 1 * float4(x) -> float4(x)
886 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400887 delete_left(&b, iter, outUpdated, outNeedsRescan);
888 }
889 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400890 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400891 if (leftType.typeKind() == Type::TypeKind::kScalar &&
892 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400893 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400894 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400895 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
896 } else {
897 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400898 // float4(0) * x -> float4(0)
899 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400900 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500901 delete_right(&b, iter, outUpdated, outNeedsRescan);
902 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400903 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400904 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400905 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400906 if (leftType.typeKind() == Type::TypeKind::kScalar &&
907 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400908 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400909 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
910 } else {
911 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400912 // float4(x) * 1 -> float4(x)
913 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400914 delete_right(&b, iter, outUpdated, outNeedsRescan);
915 }
916 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400917 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400918 if (leftType.typeKind() == Type::TypeKind::kVector &&
919 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400920 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400921 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400922 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
923 } else {
924 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400925 // x * float4(0) -> float4(0)
926 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400927 if (!left.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500928 delete_left(&b, iter, outUpdated, outNeedsRescan);
929 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400930 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400931 }
932 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400933 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400934 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400935 if (leftType.typeKind() == Type::TypeKind::kVector &&
936 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400938 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
939 } else {
940 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // 0 + float4(x) -> float4(x)
942 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400943 delete_left(&b, iter, outUpdated, outNeedsRescan);
944 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400945 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400946 if (leftType.typeKind() == Type::TypeKind::kScalar &&
947 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400948 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400949 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
950 } else {
951 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400952 // float4(x) + 0 -> float4(x)
953 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400954 delete_right(&b, iter, outUpdated, outNeedsRescan);
955 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400956 }
957 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400958 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400959 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400960 if (leftType.typeKind() == Type::TypeKind::kScalar &&
961 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400962 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400963 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
964 } else {
965 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400966 // float4(x) - 0 -> float4(x)
967 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400968 delete_right(&b, iter, outUpdated, outNeedsRescan);
969 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400970 }
971 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400972 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400973 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400974 if (leftType.typeKind() == Type::TypeKind::kScalar &&
975 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400976 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400977 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
978 } else {
979 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 // float4(x) / 1 -> float4(x)
981 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400982 delete_right(&b, iter, outUpdated, outNeedsRescan);
983 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400984 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400985 if (leftType.typeKind() == Type::TypeKind::kScalar &&
986 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400987 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400988 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400989 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
990 } else {
991 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400992 // float4(0) / x -> float4(0)
993 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400994 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500995 delete_right(&b, iter, outUpdated, outNeedsRescan);
996 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400997 }
998 }
999 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001000 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001001 if (is_constant(right, 0)) {
1002 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001003 delete_right(&b, iter, outUpdated, outNeedsRescan);
1004 }
1005 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001006 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001007 if (is_constant(right, 0)) {
1008 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001009 delete_right(&b, iter, outUpdated, outNeedsRescan);
1010 }
1011 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001012 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001013 if (is_constant(right, 1)) {
1014 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001015 delete_right(&b, iter, outUpdated, outNeedsRescan);
1016 }
1017 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001018 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001019 if (is_constant(right, 1)) {
1020 clear_write(left);
Ethan Nicholascb670962017-04-20 19:31:52 -04001021 delete_right(&b, iter, outUpdated, outNeedsRescan);
1022 }
1023 break;
1024 default:
1025 break;
1026 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001027 break;
1028 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001029 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001030 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001031 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001032 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001033 bool identity = true;
1034 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1035 if (s.fComponents[i] != i) {
1036 identity = false;
1037 break;
1038 }
1039 }
1040 if (identity) {
1041 *outUpdated = true;
1042 if (!try_replace_expression(&b, iter, &s.fBase)) {
1043 *outNeedsRescan = true;
1044 return;
1045 }
John Stiles70025e52020-09-28 16:08:58 -04001046 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001047 break;
1048 }
1049 }
1050 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001051 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001052 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001053 std::vector<int> final;
1054 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001055 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001056 }
1057 *outUpdated = true;
1058 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1059 std::move(final)));
1060 if (!try_replace_expression(&b, iter, &replacement)) {
1061 *outNeedsRescan = true;
1062 return;
1063 }
John Stiles70025e52020-09-28 16:08:58 -04001064 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001065 }
John Stiles30212b72020-06-11 17:55:07 -04001066 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001067 }
1068 default:
1069 break;
1070 }
1071}
1072
John Stiles92219b42020-06-15 12:32:24 -04001073// Returns true if this statement could potentially execute a break at the current level. We ignore
1074// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001075static bool contains_conditional_break(Statement& stmt) {
1076 class ContainsConditionalBreak : public ProgramVisitor {
1077 public:
1078 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001079 switch (stmt.kind()) {
1080 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001081 return this->INHERITED::visitStatement(stmt);
1082
Ethan Nicholase6592142020-09-08 10:22:09 -04001083 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001084 return fInConditional > 0;
1085
Ethan Nicholase6592142020-09-08 10:22:09 -04001086 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001087 ++fInConditional;
1088 bool result = this->INHERITED::visitStatement(stmt);
1089 --fInConditional;
1090 return result;
1091 }
1092
1093 default:
1094 return false;
1095 }
1096 }
1097
1098 int fInConditional = 0;
1099 using INHERITED = ProgramVisitor;
1100 };
1101
1102 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001103}
1104
Ethan Nicholas5005a222018-08-24 13:06:27 -04001105// returns true if this statement definitely executes a break at the current level (we ignore
1106// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001107static bool contains_unconditional_break(Statement& stmt) {
1108 class ContainsUnconditionalBreak : public ProgramVisitor {
1109 public:
1110 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001111 switch (stmt.kind()) {
1112 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001113 return this->INHERITED::visitStatement(stmt);
1114
Ethan Nicholase6592142020-09-08 10:22:09 -04001115 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001116 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001117
1118 default:
1119 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001120 }
John Stilesb92641c2020-08-31 18:09:01 -04001121 }
John Stiles92219b42020-06-15 12:32:24 -04001122
John Stilesb92641c2020-08-31 18:09:01 -04001123 using INHERITED = ProgramVisitor;
1124 };
John Stiles92219b42020-06-15 12:32:24 -04001125
John Stilesb92641c2020-08-31 18:09:01 -04001126 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001127}
1128
John Stiles92219b42020-06-15 12:32:24 -04001129static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1130 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001131 switch (stmt->kind()) {
1132 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001133 // Recurse into the block.
1134 Block& block = static_cast<Block&>(*stmt);
1135
1136 std::vector<std::unique_ptr<Statement>> blockStmts;
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001137 blockStmts.reserve(block.children().size());
1138 for (std::unique_ptr<Statement>& stmt : block.children()) {
1139 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001140 }
John Stiles92219b42020-06-15 12:32:24 -04001141
1142 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001143 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001144 break;
John Stiles92219b42020-06-15 12:32:24 -04001145 }
1146
Ethan Nicholase6592142020-09-08 10:22:09 -04001147 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001148 // Do not append a break to the target.
1149 break;
1150
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001151 default:
John Stiles92219b42020-06-15 12:32:24 -04001152 // Append normal statements to the target.
1153 target->push_back(std::move(stmt));
1154 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001155 }
1156}
1157
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001158// Returns a block containing all of the statements that will be run if the given case matches
1159// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1160// broken by this call and must then be discarded).
1161// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1162// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001163static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1164 SwitchCase* caseToCapture) {
1165 // We have to be careful to not move any of the pointers until after we're sure we're going to
1166 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1167 // of action. First, find the switch-case we are interested in.
1168 auto iter = switchStatement->fCases.begin();
1169 for (; iter != switchStatement->fCases.end(); ++iter) {
1170 if (iter->get() == caseToCapture) {
1171 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001172 }
John Stiles92219b42020-06-15 12:32:24 -04001173 }
1174
1175 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1176 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1177 // statements that we can use for simplification.
1178 auto startIter = iter;
1179 Statement* unconditionalBreakStmt = nullptr;
1180 for (; iter != switchStatement->fCases.end(); ++iter) {
1181 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1182 if (contains_conditional_break(*stmt)) {
1183 // We can't reduce switch-cases to a block when they have conditional breaks.
1184 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001185 }
John Stiles92219b42020-06-15 12:32:24 -04001186
1187 if (contains_unconditional_break(*stmt)) {
1188 // We found an unconditional break. We can use this block, but we need to strip
1189 // out the break statement.
1190 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001191 break;
1192 }
1193 }
John Stiles92219b42020-06-15 12:32:24 -04001194
1195 if (unconditionalBreakStmt != nullptr) {
1196 break;
1197 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001198 }
John Stiles92219b42020-06-15 12:32:24 -04001199
1200 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1201 // that we need to move over, and we know it's safe to do so.
1202 std::vector<std::unique_ptr<Statement>> caseStmts;
1203
1204 // We can move over most of the statements as-is.
1205 while (startIter != iter) {
1206 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1207 caseStmts.push_back(std::move(stmt));
1208 }
1209 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001210 }
John Stiles92219b42020-06-15 12:32:24 -04001211
1212 // If we found an unconditional break at the end, we need to move what we can while avoiding
1213 // that break.
1214 if (unconditionalBreakStmt != nullptr) {
1215 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1216 if (stmt.get() == unconditionalBreakStmt) {
1217 move_all_but_break(stmt, &caseStmts);
1218 unconditionalBreakStmt = nullptr;
1219 break;
1220 }
1221
1222 caseStmts.push_back(std::move(stmt));
1223 }
1224 }
1225
1226 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1227
1228 // Return our newly-synthesized block.
1229 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001230}
1231
Ethan Nicholascb670962017-04-20 19:31:52 -04001232void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001233 BasicBlock& b,
1234 std::vector<BasicBlock::Node>::iterator* iter,
1235 std::unordered_set<const Variable*>* undefinedVariables,
1236 bool* outUpdated,
1237 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001238 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001239 switch (stmt->kind()) {
1240 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001241 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001242 if (varDecl.fVar->dead() &&
1243 (!varDecl.fValue ||
1244 !varDecl.fValue->hasSideEffects())) {
1245 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001246 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001247 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1248 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001249 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001250 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001251 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001252 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001253 }
1254 break;
1255 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001256 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001257 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001258 if (i.test()->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001259 // constant if, collapse down to a single branch
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001260 if (i.test()->as<BoolLiteral>().value()) {
1261 SkASSERT(i.ifTrue());
1262 (*iter)->setStatement(std::move(i.ifTrue()));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001263 } else {
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001264 if (i.ifFalse()) {
1265 (*iter)->setStatement(std::move(i.ifFalse()));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001266 } else {
1267 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1268 }
1269 }
1270 *outUpdated = true;
1271 *outNeedsRescan = true;
1272 break;
1273 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001274 if (i.ifFalse() && i.ifFalse()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001275 // else block doesn't do anything, remove it
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001276 i.ifFalse().reset();
Ethan Nicholascb670962017-04-20 19:31:52 -04001277 *outUpdated = true;
1278 *outNeedsRescan = true;
1279 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001280 if (!i.ifFalse() && i.ifTrue()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001281 // if block doesn't do anything, no else block
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001282 if (i.test()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001283 // test has side effects, keep it
1284 (*iter)->setStatement(std::unique_ptr<Statement>(
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001285 new ExpressionStatement(std::move(i.test()))));
Ethan Nicholascb670962017-04-20 19:31:52 -04001286 } else {
1287 // no if, no else, no test side effects, kill the whole if
1288 // statement
1289 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1290 }
1291 *outUpdated = true;
1292 *outNeedsRescan = true;
1293 }
1294 break;
1295 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001296 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001297 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001298 int64_t switchValue;
1299 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001300 // switch is constant, replace it with the case that matches
1301 bool found = false;
1302 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001303 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001304 if (!c->fValue) {
1305 defaultCase = c.get();
1306 continue;
1307 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001308 int64_t caseValue;
1309 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1310 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001311 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1312 if (newBlock) {
1313 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001314 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 break;
1316 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001317 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001318 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001319 "static switch contains non-static conditional break");
1320 s.fIsStatic = false;
1321 }
1322 return; // can't simplify
1323 }
1324 }
1325 }
1326 if (!found) {
1327 // no matching case. use default if it exists, or kill the whole thing
1328 if (defaultCase) {
1329 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1330 if (newBlock) {
1331 (*iter)->setStatement(std::move(newBlock));
1332 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001333 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001334 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001335 "static switch contains non-static conditional break");
1336 s.fIsStatic = false;
1337 }
1338 return; // can't simplify
1339 }
1340 } else {
1341 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1342 }
1343 }
1344 *outUpdated = true;
1345 *outNeedsRescan = true;
1346 }
1347 break;
1348 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001349 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001350 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001351 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001352 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001353 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001354 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001355 *outNeedsRescan = true;
1356 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001357 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001358 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1359 *outUpdated = true;
1360 }
1361 break;
1362 }
1363 default:
1364 break;
1365 }
1366}
1367
John Stiles0cc193a2020-09-09 09:39:34 -04001368bool Compiler::scanCFG(FunctionDefinition& f) {
1369 bool madeChanges = false;
1370
Ethan Nicholascb670962017-04-20 19:31:52 -04001371 CFG cfg = CFGGenerator().getCFG(f);
1372 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001373
1374 // check for unreachable code
1375 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001376 const BasicBlock& block = cfg.fBlocks[i];
John Stiles61e75e32020-10-01 15:42:37 -04001377 if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001378 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001379 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001380 if (node.isStatement()) {
1381 offset = (*node.statement())->fOffset;
1382 } else {
1383 offset = (*node.expression())->fOffset;
1384 if ((*node.expression())->is<BoolLiteral>()) {
1385 // Function inlining can generate do { ... } while(false) loops which always
1386 // break, so the boolean condition is considered unreachable. Since not being
1387 // able to reach a literal is a non-issue in the first place, we don't report an
1388 // error in this case.
1389 continue;
1390 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001391 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001392 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001393 }
1394 }
1395 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001396 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001397 }
1398
Ethan Nicholascb670962017-04-20 19:31:52 -04001399 // check for dead code & undefined variables, perform constant propagation
1400 std::unordered_set<const Variable*> undefinedVariables;
1401 bool updated;
1402 bool needsRescan = false;
1403 do {
1404 if (needsRescan) {
1405 cfg = CFGGenerator().getCFG(f);
1406 this->computeDataFlow(&cfg);
1407 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001408 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001409
1410 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001411 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001412 for (BasicBlock& b : cfg.fBlocks) {
John Stiles61e75e32020-10-01 15:42:37 -04001413 if (!first && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001414 // Block was reachable before optimization, but has since become unreachable. In
1415 // addition to being dead code, it's broken - since control flow can't reach it, no
1416 // prior variable definitions can reach it, and therefore variables might look to
Brian Osmanc0213602020-10-06 14:43:32 -04001417 // have not been properly assigned. Kill it by replacing all statements with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001418 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001419 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles0cc193a2020-09-09 09:39:34 -04001420 node.setStatement(std::make_unique<Nop>());
1421 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001422 }
1423 }
1424 continue;
1425 }
1426 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001427 DefinitionMap definitions = b.fBefore;
1428
1429 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001430 if (iter->isExpression()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001431 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1432 &needsRescan);
1433 } else {
1434 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001435 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001436 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001437 if (needsRescan) {
1438 break;
1439 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001440 this->addDefinitions(*iter, &definitions);
1441 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001442
1443 if (needsRescan) {
1444 break;
1445 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001446 }
John Stiles0cc193a2020-09-09 09:39:34 -04001447 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001448 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001449 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001450
Ethan Nicholas91a10532017-06-22 11:24:38 -04001451 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001452 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas91a10532017-06-22 11:24:38 -04001453 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001454 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001455 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001456 switch (s.kind()) {
1457 case Statement::Kind::kIf:
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001458 if (s.as<IfStatement>().isStatic() &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001459 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001460 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001461 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001462 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001463 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001464 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001465 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001466 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001467 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001468 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001469 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001470 break;
1471 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001472 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001473 break;
1474 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001475 } else {
1476 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001477 }
1478 }
1479 }
1480
ethannicholas22f939e2016-10-13 13:25:34 -07001481 // check for missing return
Ethan Nicholased84b732020-10-08 11:45:44 -04001482 if (f.fDeclaration.returnType() != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001483 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04001484 this->error(f.fOffset, String("function '" + String(f.fDeclaration.name()) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001485 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001486 }
1487 }
John Stiles0cc193a2020-09-09 09:39:34 -04001488
1489 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001490}
1491
Brian Osman32d53552020-09-23 13:55:20 -04001492std::unique_ptr<Program> Compiler::convertProgram(
1493 Program::Kind kind,
1494 String text,
1495 const Program::Settings& settings,
1496 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1497 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001498
ethannicholasb3058bd2016-07-01 08:22:01 -07001499 fErrorText = "";
1500 fErrorCount = 0;
Brian Osman3d87e9f2020-10-08 11:50:22 -04001501 fInliner.reset(fContext.get(), fIRGenerator->fModifiers.get(), &settings);
ethannicholasd598f792016-07-25 10:08:54 -07001502 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001503 switch (kind) {
1504 case Program::kVertex_Kind:
Brian Osman3d87e9f2020-10-08 11:50:22 -04001505 fIRGenerator->start(&settings, fVertexModule);
ethannicholasb3058bd2016-07-01 08:22:01 -07001506 break;
1507 case Program::kFragment_Kind:
Brian Osman3d87e9f2020-10-08 11:50:22 -04001508 fIRGenerator->start(&settings, fFragmentModule);
ethannicholasb3058bd2016-07-01 08:22:01 -07001509 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001510 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001511 this->loadGeometryIntrinsics();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001512 fIRGenerator->start(&settings, fGeometryModule);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001513 break;
Brian Osman8e2ef022020-09-30 13:26:43 -04001514 case Program::kFragmentProcessor_Kind:
1515 this->loadFPIntrinsics();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001516 fIRGenerator->start(&settings, fFPModule);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001517 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001518 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001519 this->loadPipelineIntrinsics();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001520 fIRGenerator->start(&settings, fPipelineModule);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001521 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001522 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001523 this->loadInterpreterIntrinsics();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001524 fIRGenerator->start(&settings, fInterpreterModule);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001525 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001526 }
Brian Osman32d53552020-09-23 13:55:20 -04001527 if (externalValues) {
1528 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1529 // we're only making these visible to the current Program.
1530 for (const auto& ev : *externalValues) {
John Stilesb8cc6652020-10-08 09:12:07 -04001531 fIRGenerator->fSymbolTable->addWithoutOwnership(ev.get());
Brian Osman32d53552020-09-23 13:55:20 -04001532 }
1533 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001534 std::unique_ptr<String> textPtr(new String(std::move(text)));
1535 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001536 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001537 auto result = std::make_unique<Program>(kind,
1538 std::move(textPtr),
1539 settings,
1540 fContext,
John Stilesfbd050b2020-08-03 13:21:46 -04001541 std::move(elements),
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001542 fIRGenerator->releaseModifiers(),
John Stilesfbd050b2020-08-03 13:21:46 -04001543 fIRGenerator->fSymbolTable,
1544 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001545 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001546 if (fErrorCount) {
1547 return nullptr;
1548 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001549 if (settings.fOptimize && !this->optimize(*result)) {
1550 return nullptr;
1551 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001552 return result;
1553}
1554
Ethan Nicholas00543112018-07-31 09:44:36 -04001555bool Compiler::optimize(Program& program) {
1556 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001557 fIRGenerator->fKind = program.fKind;
1558 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001559
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001560 while (fErrorCount == 0) {
1561 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001562
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001563 // Scan and optimize based on the control-flow graph for each function.
Brian Osman1179fcf2020-10-08 16:04:40 -04001564 for (const auto& element : program.elements()) {
1565 if (element->is<FunctionDefinition>()) {
1566 madeChanges |= this->scanCFG(element->as<FunctionDefinition>());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001567 }
1568 }
1569
1570 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001571 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001572
1573 // Remove dead functions. We wait until after analysis so that we still report errors,
1574 // even in unused code.
1575 if (program.fSettings.fRemoveDeadFunctions) {
1576 program.fElements.erase(
1577 std::remove_if(program.fElements.begin(),
1578 program.fElements.end(),
1579 [&](const std::unique_ptr<ProgramElement>& element) {
1580 if (!element->is<FunctionDefinition>()) {
1581 return false;
1582 }
1583 const auto& fn = element->as<FunctionDefinition>();
Ethan Nicholased84b732020-10-08 11:45:44 -04001584 bool dead = fn.fDeclaration.callCount() == 0 &&
Ethan Nicholase2c49992020-10-05 11:49:11 -04001585 fn.fDeclaration.name() != "main";
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001586 madeChanges |= dead;
1587 return dead;
1588 }),
1589 program.fElements.end());
1590 }
1591
1592 if (program.fKind != Program::kFragmentProcessor_Kind) {
Brian Osmanc0213602020-10-06 14:43:32 -04001593 // Remove declarations of dead global variables
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001594 program.fElements.erase(
1595 std::remove_if(program.fElements.begin(), program.fElements.end(),
1596 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osmanc0213602020-10-06 14:43:32 -04001597 if (!element->is<GlobalVarDeclaration>()) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001598 return false;
1599 }
Brian Osmanc0213602020-10-06 14:43:32 -04001600 const auto& varDecl = element->as<GlobalVarDeclaration>();
1601 bool dead = varDecl.fDecl->fVar->dead();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001602 madeChanges |= dead;
1603 return dead;
1604 }),
1605 program.fElements.end());
1606 }
John Stiles73a6bff2020-09-09 13:40:37 -04001607
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001608 if (!madeChanges) {
1609 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001610 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001611 }
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001612 program.finish();
Ethan Nicholas00543112018-07-31 09:44:36 -04001613 return fErrorCount == 0;
1614}
1615
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001616#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1617
Ethan Nicholas00543112018-07-31 09:44:36 -04001618bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001619#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001620 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001621 fSource = program.fSource.get();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001622 SPIRVCodeGenerator cg(fContext.get(), fIRGenerator->fModifiers.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001623 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001624 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001625 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001626 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001627 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001628 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001629 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1630 SkDebugf("SPIR-V validation error: %s\n", m);
1631 };
1632 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001633 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001634 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001635 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001636 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001637 }
1638#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001639 fSource = program.fSource.get();
Brian Osman3d87e9f2020-10-08 11:50:22 -04001640 SPIRVCodeGenerator cg(fContext.get(), fIRGenerator->fModifiers.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001641 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001642 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001643#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001644 return result;
1645}
1646
Ethan Nicholas00543112018-07-31 09:44:36 -04001647bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001648 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001649 bool result = this->toSPIRV(program, buffer);
1650 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001651 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001652 }
1653 return result;
1654}
1655
Ethan Nicholas00543112018-07-31 09:44:36 -04001656bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001657 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001658 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001659 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001660 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001661 return result;
1662}
1663
Ethan Nicholas00543112018-07-31 09:44:36 -04001664bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001665 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001666 bool result = this->toGLSL(program, buffer);
1667 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001668 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001669 }
1670 return result;
1671}
1672
Brian Osmanc0243912020-02-19 15:35:26 -05001673bool Compiler::toHLSL(Program& program, String* out) {
1674 String spirv;
1675 if (!this->toSPIRV(program, &spirv)) {
1676 return false;
1677 }
1678
1679 return SPIRVtoHLSL(spirv, out);
1680}
1681
Ethan Nicholas00543112018-07-31 09:44:36 -04001682bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001683 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001684 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001685 return result;
1686}
1687
Ethan Nicholas00543112018-07-31 09:44:36 -04001688bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001689 StringStream buffer;
1690 bool result = this->toMetal(program, buffer);
1691 if (result) {
1692 *out = buffer.str();
1693 }
1694 return result;
1695}
1696
Greg Daniela28ea672020-09-25 11:12:56 -04001697#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001698bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001699 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001700 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001701 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001702 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001703 return result;
1704}
1705
Ethan Nicholas00543112018-07-31 09:44:36 -04001706bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001707 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001708 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001709 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001710 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001711 return result;
1712}
Greg Daniela28ea672020-09-25 11:12:56 -04001713#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001714
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001715#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001716
1717#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001718bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001719 fSource = program.fSource.get();
1720 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001721 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001722 bool result = cg.generateCode();
1723 fSource = nullptr;
1724 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001725 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001726 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001727 return result;
1728}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001729#endif
1730
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001731std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001732#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001733 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001734 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001735 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1736 bool success = cg.generateCode();
1737 fSource = nullptr;
1738 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001739 return result;
1740 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001741#else
1742 ABORT("ByteCode interpreter not enabled");
1743#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001744 return nullptr;
1745}
1746
Brian Osman401a0092020-09-10 14:47:24 -04001747const char* Compiler::OperatorName(Token::Kind op) {
1748 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001749 case Token::Kind::TK_PLUS: return "+";
1750 case Token::Kind::TK_MINUS: return "-";
1751 case Token::Kind::TK_STAR: return "*";
1752 case Token::Kind::TK_SLASH: return "/";
1753 case Token::Kind::TK_PERCENT: return "%";
1754 case Token::Kind::TK_SHL: return "<<";
1755 case Token::Kind::TK_SHR: return ">>";
1756 case Token::Kind::TK_LOGICALNOT: return "!";
1757 case Token::Kind::TK_LOGICALAND: return "&&";
1758 case Token::Kind::TK_LOGICALOR: return "||";
1759 case Token::Kind::TK_LOGICALXOR: return "^^";
1760 case Token::Kind::TK_BITWISENOT: return "~";
1761 case Token::Kind::TK_BITWISEAND: return "&";
1762 case Token::Kind::TK_BITWISEOR: return "|";
1763 case Token::Kind::TK_BITWISEXOR: return "^";
1764 case Token::Kind::TK_EQ: return "=";
1765 case Token::Kind::TK_EQEQ: return "==";
1766 case Token::Kind::TK_NEQ: return "!=";
1767 case Token::Kind::TK_LT: return "<";
1768 case Token::Kind::TK_GT: return ">";
1769 case Token::Kind::TK_LTEQ: return "<=";
1770 case Token::Kind::TK_GTEQ: return ">=";
1771 case Token::Kind::TK_PLUSEQ: return "+=";
1772 case Token::Kind::TK_MINUSEQ: return "-=";
1773 case Token::Kind::TK_STAREQ: return "*=";
1774 case Token::Kind::TK_SLASHEQ: return "/=";
1775 case Token::Kind::TK_PERCENTEQ: return "%=";
1776 case Token::Kind::TK_SHLEQ: return "<<=";
1777 case Token::Kind::TK_SHREQ: return ">>=";
1778 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1779 case Token::Kind::TK_LOGICALOREQ: return "||=";
1780 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1781 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1782 case Token::Kind::TK_BITWISEOREQ: return "|=";
1783 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1784 case Token::Kind::TK_PLUSPLUS: return "++";
1785 case Token::Kind::TK_MINUSMINUS: return "--";
1786 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001787 default:
Brian Osman401a0092020-09-10 14:47:24 -04001788 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001789 }
1790}
1791
1792
1793bool Compiler::IsAssignment(Token::Kind op) {
1794 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001795 case Token::Kind::TK_EQ: // fall through
1796 case Token::Kind::TK_PLUSEQ: // fall through
1797 case Token::Kind::TK_MINUSEQ: // fall through
1798 case Token::Kind::TK_STAREQ: // fall through
1799 case Token::Kind::TK_SLASHEQ: // fall through
1800 case Token::Kind::TK_PERCENTEQ: // fall through
1801 case Token::Kind::TK_SHLEQ: // fall through
1802 case Token::Kind::TK_SHREQ: // fall through
1803 case Token::Kind::TK_BITWISEOREQ: // fall through
1804 case Token::Kind::TK_BITWISEXOREQ: // fall through
1805 case Token::Kind::TK_BITWISEANDEQ: // fall through
1806 case Token::Kind::TK_LOGICALOREQ: // fall through
1807 case Token::Kind::TK_LOGICALXOREQ: // fall through
1808 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001809 return true;
1810 default:
1811 return false;
1812 }
1813}
1814
Brian Osman401a0092020-09-10 14:47:24 -04001815Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1816 switch (op) {
1817 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1818 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1819 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1820 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1821 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1822 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1823 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1824 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1825 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1826 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1827 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1828 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1829 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1830 default: return op;
1831 }
1832}
1833
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001834Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001835 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001836 int line = 1;
1837 int column = 1;
1838 for (int i = 0; i < offset; i++) {
1839 if ((*fSource)[i] == '\n') {
1840 ++line;
1841 column = 1;
1842 }
1843 else {
1844 ++column;
1845 }
1846 }
1847 return Position(line, column);
1848}
1849
1850void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001851 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001852 Position pos = this->position(offset);
1853 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001854}
1855
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001856String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001857 this->writeErrorCount();
1858 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001859 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001860 return result;
1861}
1862
1863void Compiler::writeErrorCount() {
1864 if (fErrorCount) {
1865 fErrorText += to_string(fErrorCount) + " error";
1866 if (fErrorCount > 1) {
1867 fErrorText += "s";
1868 }
1869 fErrorText += "\n";
1870 }
1871}
1872
John Stilesa6841be2020-08-06 14:11:56 -04001873} // namespace SkSL