blob: 327f8b8f6d05e793ce152068938e63cc2fe16695 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/sksl/SkSLByteCodeGenerator.h"
14#include "src/sksl/SkSLCFGGenerator.h"
15#include "src/sksl/SkSLCPPCodeGenerator.h"
16#include "src/sksl/SkSLGLSLCodeGenerator.h"
17#include "src/sksl/SkSLHCodeGenerator.h"
18#include "src/sksl/SkSLIRGenerator.h"
19#include "src/sksl/SkSLMetalCodeGenerator.h"
20#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040021#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050023#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/sksl/ir/SkSLEnum.h"
25#include "src/sksl/ir/SkSLExpression.h"
26#include "src/sksl/ir/SkSLExpressionStatement.h"
27#include "src/sksl/ir/SkSLFunctionCall.h"
28#include "src/sksl/ir/SkSLIntLiteral.h"
29#include "src/sksl/ir/SkSLModifiersDeclaration.h"
30#include "src/sksl/ir/SkSLNop.h"
31#include "src/sksl/ir/SkSLSymbolTable.h"
32#include "src/sksl/ir/SkSLTernaryExpression.h"
33#include "src/sksl/ir/SkSLUnresolvedFunction.h"
34#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070035
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040036#include <fstream>
37
Ethan Nicholasa11035b2019-11-26 16:27:47 -050038#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
39#include "include/gpu/GrContextOptions.h"
40#include "src/gpu/GrShaderCaps.h"
41#endif
42
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040043#ifdef SK_ENABLE_SPIRV_VALIDATION
44#include "spirv-tools/libspirv.hpp"
45#endif
46
Brian Osmandd496172020-08-08 08:17:18 -040047#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040048
49#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
50#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
51#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
52#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
53#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
54#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
55#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
56
57#else
58
Brian Osmandd496172020-08-08 08:17:18 -040059// GN generates or copies all of these files to the skslc executable directory
60static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
61static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
62static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
63static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
64static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
65static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
66static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040067
68#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040069
ethannicholasb3058bd2016-07-01 08:22:01 -070070namespace SkSL {
71
Ethan Nicholasdb80f692019-11-22 14:06:12 -050072static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Brian Osman08f986d2020-05-13 17:06:46 -040073 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
74 for (auto iter = src->begin(); iter != src->end(); ) {
75 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050076 switch (element->fKind) {
77 case ProgramElement::kFunction_Kind: {
78 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osman08f986d2020-05-13 17:06:46 -040079 SkASSERT(f.fDeclaration.fBuiltin);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040080 String key = f.fDeclaration.description();
Brian Osman08f986d2020-05-13 17:06:46 -040081 SkASSERT(target->find(key) == target->end());
82 (*target)[key] = std::make_pair(std::move(element), false);
83 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050084 break;
85 }
86 case ProgramElement::kEnum_Kind: {
87 Enum& e = (Enum&) *element;
88 StringFragment name = e.fTypeName;
89 SkASSERT(target->find(name) == target->end());
90 (*target)[name] = std::make_pair(std::move(element), false);
Brian Osman08f986d2020-05-13 17:06:46 -040091 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050092 break;
93 }
94 default:
95 printf("unsupported include file element\n");
96 SkASSERT(false);
97 }
98 }
99}
100
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400101Compiler::Compiler(Flags flags)
102: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400103, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400104, fErrorCount(0) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400105 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400106 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400107 #define ADD_TYPE(t) symbols->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
108 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700109 ADD_TYPE(Void);
110 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400111 ADD_TYPE(Float2);
112 ADD_TYPE(Float3);
113 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400114 ADD_TYPE(Half);
115 ADD_TYPE(Half2);
116 ADD_TYPE(Half3);
117 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700118 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400119 ADD_TYPE(Int2);
120 ADD_TYPE(Int3);
121 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700122 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400123 ADD_TYPE(UInt2);
124 ADD_TYPE(UInt3);
125 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400126 ADD_TYPE(Short);
127 ADD_TYPE(Short2);
128 ADD_TYPE(Short3);
129 ADD_TYPE(Short4);
130 ADD_TYPE(UShort);
131 ADD_TYPE(UShort2);
132 ADD_TYPE(UShort3);
133 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400134 ADD_TYPE(Byte);
135 ADD_TYPE(Byte2);
136 ADD_TYPE(Byte3);
137 ADD_TYPE(Byte4);
138 ADD_TYPE(UByte);
139 ADD_TYPE(UByte2);
140 ADD_TYPE(UByte3);
141 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700142 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400143 ADD_TYPE(Bool2);
144 ADD_TYPE(Bool3);
145 ADD_TYPE(Bool4);
146 ADD_TYPE(Float2x2);
147 ADD_TYPE(Float2x3);
148 ADD_TYPE(Float2x4);
149 ADD_TYPE(Float3x2);
150 ADD_TYPE(Float3x3);
151 ADD_TYPE(Float3x4);
152 ADD_TYPE(Float4x2);
153 ADD_TYPE(Float4x3);
154 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400155 ADD_TYPE(Half2x2);
156 ADD_TYPE(Half2x3);
157 ADD_TYPE(Half2x4);
158 ADD_TYPE(Half3x2);
159 ADD_TYPE(Half3x3);
160 ADD_TYPE(Half3x4);
161 ADD_TYPE(Half4x2);
162 ADD_TYPE(Half4x3);
163 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700164 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400165 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 ADD_TYPE(GenIType);
167 ADD_TYPE(GenUType);
168 ADD_TYPE(GenBType);
169 ADD_TYPE(Mat);
170 ADD_TYPE(Vec);
171 ADD_TYPE(GVec);
172 ADD_TYPE(GVec2);
173 ADD_TYPE(GVec3);
174 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400175 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 ADD_TYPE(IVec);
177 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400178 ADD_TYPE(SVec);
179 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400180 ADD_TYPE(ByteVec);
181 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182 ADD_TYPE(BVec);
183
184 ADD_TYPE(Sampler1D);
185 ADD_TYPE(Sampler2D);
186 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700187 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 ADD_TYPE(SamplerCube);
189 ADD_TYPE(Sampler2DRect);
190 ADD_TYPE(Sampler1DArray);
191 ADD_TYPE(Sampler2DArray);
192 ADD_TYPE(SamplerCubeArray);
193 ADD_TYPE(SamplerBuffer);
194 ADD_TYPE(Sampler2DMS);
195 ADD_TYPE(Sampler2DMSArray);
196
Brian Salomonbf7b6202016-11-11 16:08:03 -0500197 ADD_TYPE(ISampler2D);
198
Brian Salomon2a51de82016-11-16 12:06:01 -0500199 ADD_TYPE(Image2D);
200 ADD_TYPE(IImage2D);
201
Greg Daniel64773e62016-11-22 09:44:03 -0500202 ADD_TYPE(SubpassInput);
203 ADD_TYPE(SubpassInputMS);
204
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 ADD_TYPE(GSampler1D);
206 ADD_TYPE(GSampler2D);
207 ADD_TYPE(GSampler3D);
208 ADD_TYPE(GSamplerCube);
209 ADD_TYPE(GSampler2DRect);
210 ADD_TYPE(GSampler1DArray);
211 ADD_TYPE(GSampler2DArray);
212 ADD_TYPE(GSamplerCubeArray);
213 ADD_TYPE(GSamplerBuffer);
214 ADD_TYPE(GSampler2DMS);
215 ADD_TYPE(GSampler2DMSArray);
216
217 ADD_TYPE(Sampler1DShadow);
218 ADD_TYPE(Sampler2DShadow);
219 ADD_TYPE(SamplerCubeShadow);
220 ADD_TYPE(Sampler2DRectShadow);
221 ADD_TYPE(Sampler1DArrayShadow);
222 ADD_TYPE(Sampler2DArrayShadow);
223 ADD_TYPE(SamplerCubeArrayShadow);
224 ADD_TYPE(GSampler2DArrayShadow);
225 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400226 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400227 ADD_TYPE(Sampler);
228 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
Brian Osman28590d52020-03-23 16:59:08 -0400230 StringFragment fpAliasName("shader");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 symbols->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400232
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700233 StringFragment skCapsName("sk_Caps");
John Stiles311dd9d2020-08-13 17:09:29 -0400234 fIRGenerator->fSymbolTable->add(
235 skCapsName,
236 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
237 *fContext->fSkCaps_Type, Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500238
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500239 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
240 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400242#if SKSL_STANDALONE
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400243 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, symbols, &gpuIntrinsics,
244 &fGpuSymbolTable);
245 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
246 &fVertexInclude, &fVertexSymbolTable);
247 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
248 &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249#else
250 {
251 Rehydrator rehydrator(fContext.get(), symbols, this, SKSL_INCLUDE_sksl_gpu,
252 SKSL_INCLUDE_sksl_gpu_LENGTH);
253 fGpuSymbolTable = rehydrator.symbolTable();
254 gpuIntrinsics = rehydrator.elements();
255 }
256 {
257 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
258 SKSL_INCLUDE_sksl_vert_LENGTH);
259 fVertexSymbolTable = rehydrator.symbolTable();
260 fVertexInclude = rehydrator.elements();
261 }
262 {
263 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
264 SKSL_INCLUDE_sksl_frag_LENGTH);
265 fFragmentSymbolTable = rehydrator.symbolTable();
266 fFragmentInclude = rehydrator.elements();
267 }
268#endif
269 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
Brian Osmanb08cc022020-04-02 11:38:40 -0400270 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271}
272
273Compiler::~Compiler() {
274 delete fIRGenerator;
275}
276
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277void Compiler::loadGeometryIntrinsics() {
278 if (fGeometrySymbolTable) {
279 return;
280 }
Brian Osmandd496172020-08-08 08:17:18 -0400281 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400282 {
283 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
284 SKSL_INCLUDE_sksl_geom_LENGTH);
285 fGeometrySymbolTable = rehydrator.symbolTable();
286 fGeometryInclude = rehydrator.elements();
287 }
288 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400289 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
290 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 #endif
292}
293
294void Compiler::loadPipelineIntrinsics() {
295 if (fPipelineSymbolTable) {
296 return;
297 }
Brian Osmandd496172020-08-08 08:17:18 -0400298 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400299 {
300 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
301 SKSL_INCLUDE_sksl_pipeline,
302 SKSL_INCLUDE_sksl_pipeline_LENGTH);
303 fPipelineSymbolTable = rehydrator.symbolTable();
304 fPipelineInclude = rehydrator.elements();
305 }
306 #else
307 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400308 fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 #endif
310}
311
312void Compiler::loadInterpreterIntrinsics() {
313 if (fInterpreterSymbolTable) {
314 return;
315 }
316 this->loadPipelineIntrinsics();
Brian Osmandd496172020-08-08 08:17:18 -0400317 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400318 {
319 Rehydrator rehydrator(fContext.get(), fPipelineSymbolTable, this,
320 SKSL_INCLUDE_sksl_interp,
321 SKSL_INCLUDE_sksl_interp_LENGTH);
322 fInterpreterSymbolTable = rehydrator.symbolTable();
323 fInterpreterInclude = rehydrator.elements();
324 }
325 #else
326 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400327 fIRGenerator->fSymbolTable, &fInterpreterInclude,
328 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400329 #endif
330}
331
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400332void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400333 std::shared_ptr<SymbolTable> base,
334 std::vector<std::unique_ptr<ProgramElement>>* outElements,
335 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400336 std::ifstream in(path);
337 std::string stdText{std::istreambuf_iterator<char>(in),
338 std::istreambuf_iterator<char>()};
339 if (in.rdstate()) {
340 printf("error reading %s\n", path);
341 abort();
342 }
343 if (!base) {
344 base = fIRGenerator->fSymbolTable;
345 }
346 SkASSERT(base);
347 const String* source = base->takeOwnershipOfString(std::make_unique<String>(stdText.c_str()));
348 fSource = source;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 std::shared_ptr<SymbolTable> old = fIRGenerator->fSymbolTable;
350 if (base) {
351 fIRGenerator->fSymbolTable = std::move(base);
352 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400353 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500354#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
355 GrContextOptions opts;
356 GrShaderCaps caps(opts);
357 settings.fCaps = &caps;
358#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 SkASSERT(fIRGenerator->fCanInline);
360 fIRGenerator->fCanInline = false;
361 fIRGenerator->start(&settings, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400362 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400364 if (this->fErrorCount) {
365 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
366 }
367 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400368 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500369#ifdef SK_DEBUG
370 fSource = nullptr;
371#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 fIRGenerator->fSymbolTable = std::move(old);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400373}
374
ethannicholas22f939e2016-10-13 13:25:34 -0700375// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500376void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
377 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700378 switch (lvalue->fKind) {
379 case Expression::kVariableReference_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400380 const Variable& var = lvalue->as<VariableReference>().fVariable;
ethannicholas22f939e2016-10-13 13:25:34 -0700381 if (var.fStorage == Variable::kLocal_Storage) {
382 (*definitions)[&var] = expr;
383 }
384 break;
385 }
386 case Expression::kSwizzle_Kind:
387 // We consider the variable written to as long as at least some of its components have
388 // been written to. This will lead to some false negatives (we won't catch it if you
389 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400390 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
391 // 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 -0700392 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400393 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400394 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700395 definitions);
396 break;
397 case Expression::kIndex_Kind:
398 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400399 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400400 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700401 definitions);
402 break;
403 case Expression::kFieldAccess_Kind:
404 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400405 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400406 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700407 definitions);
408 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500409 case Expression::kTernary_Kind:
410 // To simplify analysis, we just pretend that we write to both sides of the ternary.
411 // This allows for false positives (meaning we fail to detect that a variable might not
412 // have been assigned), but is preferable to false negatives.
John Stilesa5a97b42020-08-18 11:19:07 -0400413 this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400414 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500415 definitions);
John Stilesa5a97b42020-08-18 11:19:07 -0400416 this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400417 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500418 definitions);
419 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400420 case Expression::kExternalValue_Kind:
421 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700422 default:
423 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400424 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700425 }
426}
427
428// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400429void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500430 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700431 switch (node.fKind) {
432 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400433 SkASSERT(node.expression());
John Stilesa5a97b42020-08-18 11:19:07 -0400434 Expression* expr = node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500435 switch (expr->fKind) {
436 case Expression::kBinary_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400437 BinaryExpression* b = &expr->as<BinaryExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400438 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500439 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700440 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500441 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400442 b->fLeft.get(),
443 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
444 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500445
446 }
447 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700448 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400449 case Expression::kFunctionCall_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400450 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400451 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
452 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
453 this->addDefinition(
454 c.fArguments[i].get(),
455 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
456 definitions);
457 }
458 }
459 break;
460 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500461 case Expression::kPrefix_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400462 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400463 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
464 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500465 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400466 p->fOperand.get(),
467 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
468 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500469 }
470 break;
471 }
472 case Expression::kPostfix_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400473 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400474 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
475 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500476 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400477 p->fOperand.get(),
478 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
479 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500480 }
481 break;
482 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400483 case Expression::kVariableReference_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400484 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholascb670962017-04-20 19:31:52 -0400485 if (v->fRefKind != VariableReference::kRead_RefKind) {
486 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400487 v,
488 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
489 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400490 }
John Stiles30212b72020-06-11 17:55:07 -0400491 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400492 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500493 default:
494 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700495 }
496 break;
497 }
498 case BasicBlock::Node::kStatement_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400499 Statement* stmt = node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000500 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
John Stilesa5a97b42020-08-18 11:19:07 -0400501 VarDeclaration& vd = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000502 if (vd.fValue) {
503 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700504 }
505 }
506 break;
507 }
508 }
509}
510
511void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
512 BasicBlock& block = cfg->fBlocks[blockId];
513
514 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500515 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700516 for (const BasicBlock::Node& n : block.fNodes) {
517 this->addDefinitions(n, &after);
518 }
519
520 // propagate definitions to exits
521 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400522 if (exitId == blockId) {
523 continue;
524 }
ethannicholas22f939e2016-10-13 13:25:34 -0700525 BasicBlock& exit = cfg->fBlocks[exitId];
526 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500527 std::unique_ptr<Expression>* e1 = pair.second;
528 auto found = exit.fBefore.find(pair.first);
529 if (found == exit.fBefore.end()) {
530 // exit has no definition for it, just copy it
531 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700532 exit.fBefore[pair.first] = e1;
533 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500534 // exit has a (possibly different) value already defined
535 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700536 if (e1 != e2) {
537 // definition has changed, merge and add exit block to worklist
538 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500539 if (e1 && e2) {
540 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400541 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500542 } else {
543 exit.fBefore[pair.first] = nullptr;
544 }
ethannicholas22f939e2016-10-13 13:25:34 -0700545 }
546 }
547 }
548 }
549}
550
551// returns a map which maps all local variables in the function to null, indicating that their value
552// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500553static DefinitionMap compute_start_state(const CFG& cfg) {
554 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400555 for (const auto& block : cfg.fBlocks) {
556 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700557 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400558 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400559 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700560 if (s->fKind == Statement::kVarDeclarations_Kind) {
John Stilesa5a97b42020-08-18 11:19:07 -0400561 const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000562 for (const auto& decl : vd->fDeclaration->fVars) {
563 if (decl->fKind == Statement::kVarDeclaration_Kind) {
John Stilesa5a97b42020-08-18 11:19:07 -0400564 result[decl->as<VarDeclaration>().fVar] = nullptr;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000565 }
Mike Klein6ad99092016-10-26 10:35:22 -0400566 }
ethannicholas22f939e2016-10-13 13:25:34 -0700567 }
568 }
569 }
570 }
571 return result;
572}
573
Ethan Nicholascb670962017-04-20 19:31:52 -0400574/**
575 * Returns true if assigning to this lvalue has no effect.
576 */
577static bool is_dead(const Expression& lvalue) {
578 switch (lvalue.fKind) {
579 case Expression::kVariableReference_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400580 return lvalue.as<VariableReference>().fVariable.dead();
Ethan Nicholascb670962017-04-20 19:31:52 -0400581 case Expression::kSwizzle_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400582 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholascb670962017-04-20 19:31:52 -0400583 case Expression::kFieldAccess_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400584 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholascb670962017-04-20 19:31:52 -0400585 case Expression::kIndex_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400586 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500587 return is_dead(*idx.fBase) &&
588 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400589 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500590 case Expression::kTernary_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400591 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasa583b812018-01-18 13:32:11 -0500592 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
593 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400594 case Expression::kExternalValue_Kind:
595 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500597#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400598 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500599#endif
600 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400601 }
602}
ethannicholas22f939e2016-10-13 13:25:34 -0700603
Ethan Nicholascb670962017-04-20 19:31:52 -0400604/**
605 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
606 * to a dead target and lack of side effects on the left hand side.
607 */
608static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700609 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400610 return false;
611 }
612 return is_dead(*b.fLeft);
613}
614
615void Compiler::computeDataFlow(CFG* cfg) {
616 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700617 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400618 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700619 workList.insert(i);
620 }
621 while (workList.size()) {
622 BlockId next = *workList.begin();
623 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400624 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700625 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400626}
627
628/**
629 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
630 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
631 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
632 * need to be regenerated).
633 */
John Stilesafbf8992020-08-18 10:08:21 -0400634static bool try_replace_expression(BasicBlock* b,
635 std::vector<BasicBlock::Node>::iterator* iter,
636 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400637 std::unique_ptr<Expression>* target = (*iter)->expression();
638 if (!b->tryRemoveExpression(iter)) {
639 *target = std::move(*newExpression);
640 return false;
641 }
642 *target = std::move(*newExpression);
643 return b->tryInsertExpression(iter, target);
644}
645
646/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400647 * Returns true if the expression is a constant numeric literal with the specified value, or a
648 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400649 */
John Stilesafbf8992020-08-18 10:08:21 -0400650static bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400651 switch (expr.fKind) {
652 case Expression::kIntLiteral_Kind:
John Stiles81365af2020-08-18 09:24:00 -0400653 return expr.as<IntLiteral>().fValue == value;
Ethan Nicholascb670962017-04-20 19:31:52 -0400654 case Expression::kFloatLiteral_Kind:
John Stiles81365af2020-08-18 09:24:00 -0400655 return expr.as<FloatLiteral>().fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400656 case Expression::kConstructor_Kind: {
John Stiles81365af2020-08-18 09:24:00 -0400657 const Constructor& c = expr.as<Constructor>();
Ethan Nicholasd188c182019-06-10 15:55:38 -0400658 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
659 : c.fType.isFloat();
Brian Osmanb6b95732020-06-30 11:44:27 -0400660 if (c.fType.kind() == Type::kVector_Kind && c.isCompileTimeConstant()) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400661 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400662 if (isFloat) {
663 if (c.getFVecComponent(i) != value) {
664 return false;
665 }
666 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400667 return false;
668 }
669 }
670 return true;
671 }
672 return false;
673 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400674 default:
675 return false;
676 }
677}
678
679/**
680 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
681 * and CFG structures).
682 */
John Stilesafbf8992020-08-18 10:08:21 -0400683static void delete_left(BasicBlock* b,
684 std::vector<BasicBlock::Node>::iterator* iter,
685 bool* outUpdated,
686 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400687 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400688 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400689 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400690 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400691 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400692 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400693 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400694 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400695 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400696 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400697 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400698 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400699 *outNeedsRescan = true;
700 return;
701 }
702 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400703 *outNeedsRescan = true;
704 return;
705 }
706 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400707 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
708 (*iter)->expression() != &bin.fRight) {
709 *outNeedsRescan = true;
710 return;
711 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400712 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400713 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400714}
715
716/**
717 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
718 * CFG structures).
719 */
John Stilesafbf8992020-08-18 10:08:21 -0400720static void delete_right(BasicBlock* b,
721 std::vector<BasicBlock::Node>::iterator* iter,
722 bool* outUpdated,
723 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400724 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400725 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400726 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400727 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400728 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
729 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400730 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400731 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400732 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400733 *target = std::move(bin.fLeft);
734 if (*iter == b->fNodes.begin()) {
735 *outNeedsRescan = true;
736 return;
737 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400738 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400739 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
740 (*iter)->expression() != &bin.fLeft)) {
741 *outNeedsRescan = true;
742 return;
743 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400744 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400745 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400746}
747
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400748/**
749 * Constructs the specified type using a single argument.
750 */
751static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
752 std::vector<std::unique_ptr<Expression>> args;
753 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700754 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400755 return result;
756}
757
758/**
759 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
760 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
761 */
762static void vectorize(BasicBlock* b,
763 std::vector<BasicBlock::Node>::iterator* iter,
764 const Type& type,
765 std::unique_ptr<Expression>* otherExpression,
766 bool* outUpdated,
767 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400768 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
769 SkASSERT(type.kind() == Type::kVector_Kind);
770 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400771 *outUpdated = true;
772 std::unique_ptr<Expression>* target = (*iter)->expression();
773 if (!b->tryRemoveExpression(iter)) {
774 *target = construct(type, std::move(*otherExpression));
775 *outNeedsRescan = true;
776 } else {
777 *target = construct(type, std::move(*otherExpression));
778 if (!b->tryInsertExpression(iter, target)) {
779 *outNeedsRescan = true;
780 }
781 }
782}
783
784/**
785 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
786 * left to yield vec<n>(x).
787 */
788static void vectorize_left(BasicBlock* b,
789 std::vector<BasicBlock::Node>::iterator* iter,
790 bool* outUpdated,
791 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400792 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400793 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
794}
795
796/**
797 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
798 * right to yield vec<n>(y).
799 */
800static void vectorize_right(BasicBlock* b,
801 std::vector<BasicBlock::Node>::iterator* iter,
802 bool* outUpdated,
803 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400804 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400805 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
806}
807
808// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400809static void clear_write(Expression& expr) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400810 switch (expr.fKind) {
811 case Expression::kVariableReference_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400812 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400813 break;
814 }
815 case Expression::kFieldAccess_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400816 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400817 break;
818 case Expression::kSwizzle_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400819 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400820 break;
821 case Expression::kIndex_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -0400822 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400823 break;
824 default:
825 ABORT("shouldn't be writing to this kind of expression\n");
826 break;
827 }
828}
829
Ethan Nicholascb670962017-04-20 19:31:52 -0400830void Compiler::simplifyExpression(DefinitionMap& definitions,
831 BasicBlock& b,
832 std::vector<BasicBlock::Node>::iterator* iter,
833 std::unordered_set<const Variable*>* undefinedVariables,
834 bool* outUpdated,
835 bool* outNeedsRescan) {
836 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400837 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400838 if ((*iter)->fConstantPropagation) {
839 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
840 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400841 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400842 if (!try_replace_expression(&b, iter, &optimized)) {
843 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400844 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400845 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400846 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400848 }
849 }
850 switch (expr->fKind) {
851 case Expression::kVariableReference_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400852 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400853 const Variable& var = ref.fVariable;
854 if (ref.refKind() != VariableReference::kWrite_RefKind &&
855 ref.refKind() != VariableReference::kPointer_RefKind &&
856 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400857 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
858 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000859 this->error(expr->fOffset,
860 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400861 }
862 break;
863 }
864 case Expression::kTernary_Kind: {
865 TernaryExpression* t = (TernaryExpression*) expr;
866 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
867 // ternary has a constant test, replace it with either the true or
868 // false branch
869 if (((BoolLiteral&) *t->fTest).fValue) {
870 (*iter)->setExpression(std::move(t->fIfTrue));
871 } else {
872 (*iter)->setExpression(std::move(t->fIfFalse));
873 }
874 *outUpdated = true;
875 *outNeedsRescan = true;
876 }
877 break;
878 }
879 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400880 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400881 if (dead_assignment(*bin)) {
882 delete_left(&b, iter, outUpdated, outNeedsRescan);
883 break;
884 }
885 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400886 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
887 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
888 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
889 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
890 break;
891 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400892 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400893 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400895 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
896 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400897 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400898 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
899 } else {
900 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400901 // 1 * float4(x) -> float4(x)
902 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400903 delete_left(&b, iter, outUpdated, outNeedsRescan);
904 }
905 }
906 else if (is_constant(*bin->fLeft, 0)) {
907 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500908 bin->fRight->fType.kind() == Type::kVector_Kind &&
909 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400910 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400911 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
912 } else {
913 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400914 // float4(0) * x -> float4(0)
915 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500916 if (!bin->fRight->hasSideEffects()) {
917 delete_right(&b, iter, outUpdated, outNeedsRescan);
918 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400919 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400920 }
921 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400922 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
923 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400924 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400925 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
926 } else {
927 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400928 // float4(x) * 1 -> float4(x)
929 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400930 delete_right(&b, iter, outUpdated, outNeedsRescan);
931 }
932 }
933 else if (is_constant(*bin->fRight, 0)) {
934 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500935 bin->fRight->fType.kind() == Type::kScalar_Kind &&
936 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400938 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
939 } else {
940 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // x * float4(0) -> float4(0)
942 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500943 if (!bin->fLeft->hasSideEffects()) {
944 delete_left(&b, iter, outUpdated, outNeedsRescan);
945 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400946 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400947 }
948 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400949 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400950 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400951 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
952 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400953 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400954 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
955 } else {
956 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400957 // 0 + float4(x) -> float4(x)
958 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400959 delete_left(&b, iter, outUpdated, outNeedsRescan);
960 }
961 } else if (is_constant(*bin->fRight, 0)) {
962 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
963 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400964 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400965 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
966 } else {
967 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400968 // float4(x) + 0 -> float4(x)
969 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400970 delete_right(&b, iter, outUpdated, outNeedsRescan);
971 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400972 }
973 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400974 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400975 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400976 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
977 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400978 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400979 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
980 } else {
981 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400982 // float4(x) - 0 -> float4(x)
983 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400984 delete_right(&b, iter, outUpdated, outNeedsRescan);
985 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400986 }
987 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400988 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -0400989 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400990 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
991 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400992 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400993 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
994 } else {
995 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400996 // float4(x) / 1 -> float4(x)
997 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400998 delete_right(&b, iter, outUpdated, outNeedsRescan);
999 }
1000 } else if (is_constant(*bin->fLeft, 0)) {
1001 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -05001002 bin->fRight->fType.kind() == Type::kVector_Kind &&
1003 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001004 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001005 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1006 } else {
1007 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001008 // float4(0) / x -> float4(0)
1009 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001010 if (!bin->fRight->hasSideEffects()) {
1011 delete_right(&b, iter, outUpdated, outNeedsRescan);
1012 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001013 }
1014 }
1015 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001016 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001017 if (is_constant(*bin->fRight, 0)) {
1018 clear_write(*bin->fLeft);
1019 delete_right(&b, iter, outUpdated, outNeedsRescan);
1020 }
1021 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001022 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001023 if (is_constant(*bin->fRight, 0)) {
1024 clear_write(*bin->fLeft);
1025 delete_right(&b, iter, outUpdated, outNeedsRescan);
1026 }
1027 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001028 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001029 if (is_constant(*bin->fRight, 1)) {
1030 clear_write(*bin->fLeft);
1031 delete_right(&b, iter, outUpdated, outNeedsRescan);
1032 }
1033 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001034 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 if (is_constant(*bin->fRight, 1)) {
1036 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001037 delete_right(&b, iter, outUpdated, outNeedsRescan);
1038 }
1039 break;
1040 default:
1041 break;
1042 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001043 break;
1044 }
1045 case Expression::kSwizzle_Kind: {
1046 Swizzle& s = (Swizzle&) *expr;
1047 // detect identity swizzles like foo.rgba
1048 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1049 bool identity = true;
1050 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1051 if (s.fComponents[i] != i) {
1052 identity = false;
1053 break;
1054 }
1055 }
1056 if (identity) {
1057 *outUpdated = true;
1058 if (!try_replace_expression(&b, iter, &s.fBase)) {
1059 *outNeedsRescan = true;
1060 return;
1061 }
1062 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1063 break;
1064 }
1065 }
1066 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1067 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
John Stilesa5a97b42020-08-18 11:19:07 -04001068 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001069 std::vector<int> final;
1070 for (int c : s.fComponents) {
1071 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1072 final.push_back(c);
1073 } else {
1074 final.push_back(base.fComponents[c]);
1075 }
1076 }
1077 *outUpdated = true;
1078 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1079 std::move(final)));
1080 if (!try_replace_expression(&b, iter, &replacement)) {
1081 *outNeedsRescan = true;
1082 return;
1083 }
1084 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001085 }
John Stiles30212b72020-06-11 17:55:07 -04001086 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001087 }
1088 default:
1089 break;
1090 }
1091}
1092
John Stiles92219b42020-06-15 12:32:24 -04001093// Implementation-detail recursive helper function for `contains_conditional_break`.
1094static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001095 switch (s.fKind) {
1096 case Statement::kBlock_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -04001097 for (const std::unique_ptr<Statement>& sub : s.as<Block>().fStatements) {
John Stiles92219b42020-06-15 12:32:24 -04001098 if (contains_conditional_break_impl(*sub, inConditional)) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001099 return true;
1100 }
1101 }
1102 return false;
John Stiles92219b42020-06-15 12:32:24 -04001103
Ethan Nicholas5005a222018-08-24 13:06:27 -04001104 case Statement::kBreak_Kind:
1105 return inConditional;
John Stiles92219b42020-06-15 12:32:24 -04001106
Ethan Nicholas5005a222018-08-24 13:06:27 -04001107 case Statement::kIf_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001108 const IfStatement& i = s.as<IfStatement>();
John Stiles92219b42020-06-15 12:32:24 -04001109 return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
1110 (i.fIfFalse &&
1111 contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
Ethan Nicholas5005a222018-08-24 13:06:27 -04001112 }
John Stiles92219b42020-06-15 12:32:24 -04001113
Ethan Nicholas5005a222018-08-24 13:06:27 -04001114 default:
1115 return false;
1116 }
1117}
1118
John Stiles92219b42020-06-15 12:32:24 -04001119// Returns true if this statement could potentially execute a break at the current level. We ignore
1120// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
1121static bool contains_conditional_break(Statement& s) {
1122 return contains_conditional_break_impl(s, /*inConditional=*/false);
1123}
1124
Ethan Nicholas5005a222018-08-24 13:06:27 -04001125// returns true if this statement definitely executes a break at the current level (we ignore
1126// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1127static bool contains_unconditional_break(Statement& s) {
1128 switch (s.fKind) {
1129 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001130 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001131 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001132 return true;
1133 }
1134 }
1135 return false;
John Stiles92219b42020-06-15 12:32:24 -04001136
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001137 case Statement::kBreak_Kind:
1138 return true;
John Stiles92219b42020-06-15 12:32:24 -04001139
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001140 default:
1141 return false;
1142 }
1143}
1144
John Stiles92219b42020-06-15 12:32:24 -04001145static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1146 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001147 switch (stmt->fKind) {
John Stiles92219b42020-06-15 12:32:24 -04001148 case Statement::kBlock_Kind: {
1149 // Recurse into the block.
1150 Block& block = static_cast<Block&>(*stmt);
1151
1152 std::vector<std::unique_ptr<Statement>> blockStmts;
1153 blockStmts.reserve(block.fStatements.size());
1154 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1155 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001156 }
John Stiles92219b42020-06-15 12:32:24 -04001157
1158 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1159 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001160 break;
John Stiles92219b42020-06-15 12:32:24 -04001161 }
1162
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001163 case Statement::kBreak_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001164 // Do not append a break to the target.
1165 break;
1166
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001167 default:
John Stiles92219b42020-06-15 12:32:24 -04001168 // Append normal statements to the target.
1169 target->push_back(std::move(stmt));
1170 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001171 }
1172}
1173
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001174// Returns a block containing all of the statements that will be run if the given case matches
1175// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1176// broken by this call and must then be discarded).
1177// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1178// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001179static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1180 SwitchCase* caseToCapture) {
1181 // We have to be careful to not move any of the pointers until after we're sure we're going to
1182 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1183 // of action. First, find the switch-case we are interested in.
1184 auto iter = switchStatement->fCases.begin();
1185 for (; iter != switchStatement->fCases.end(); ++iter) {
1186 if (iter->get() == caseToCapture) {
1187 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001188 }
John Stiles92219b42020-06-15 12:32:24 -04001189 }
1190
1191 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1192 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1193 // statements that we can use for simplification.
1194 auto startIter = iter;
1195 Statement* unconditionalBreakStmt = nullptr;
1196 for (; iter != switchStatement->fCases.end(); ++iter) {
1197 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1198 if (contains_conditional_break(*stmt)) {
1199 // We can't reduce switch-cases to a block when they have conditional breaks.
1200 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001201 }
John Stiles92219b42020-06-15 12:32:24 -04001202
1203 if (contains_unconditional_break(*stmt)) {
1204 // We found an unconditional break. We can use this block, but we need to strip
1205 // out the break statement.
1206 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001207 break;
1208 }
1209 }
John Stiles92219b42020-06-15 12:32:24 -04001210
1211 if (unconditionalBreakStmt != nullptr) {
1212 break;
1213 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001214 }
John Stiles92219b42020-06-15 12:32:24 -04001215
1216 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1217 // that we need to move over, and we know it's safe to do so.
1218 std::vector<std::unique_ptr<Statement>> caseStmts;
1219
1220 // We can move over most of the statements as-is.
1221 while (startIter != iter) {
1222 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1223 caseStmts.push_back(std::move(stmt));
1224 }
1225 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001226 }
John Stiles92219b42020-06-15 12:32:24 -04001227
1228 // If we found an unconditional break at the end, we need to move what we can while avoiding
1229 // that break.
1230 if (unconditionalBreakStmt != nullptr) {
1231 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1232 if (stmt.get() == unconditionalBreakStmt) {
1233 move_all_but_break(stmt, &caseStmts);
1234 unconditionalBreakStmt = nullptr;
1235 break;
1236 }
1237
1238 caseStmts.push_back(std::move(stmt));
1239 }
1240 }
1241
1242 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1243
1244 // Return our newly-synthesized block.
1245 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001246}
1247
Ethan Nicholascb670962017-04-20 19:31:52 -04001248void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001249 BasicBlock& b,
1250 std::vector<BasicBlock::Node>::iterator* iter,
1251 std::unordered_set<const Variable*>* undefinedVariables,
1252 bool* outUpdated,
1253 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001254 Statement* stmt = (*iter)->statement()->get();
1255 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001256 case Statement::kVarDeclaration_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001257 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001258 if (varDecl.fVar->dead() &&
1259 (!varDecl.fValue ||
1260 !varDecl.fValue->hasSideEffects())) {
1261 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001262 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001263 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1264 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001265 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001266 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001267 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001268 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001269 }
1270 break;
1271 }
1272 case Statement::kIf_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001273 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001274 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1275 // constant if, collapse down to a single branch
John Stilesa5a97b42020-08-18 11:19:07 -04001276 if (i.fTest->as<BoolLiteral>().fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001277 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001278 (*iter)->setStatement(std::move(i.fIfTrue));
1279 } else {
1280 if (i.fIfFalse) {
1281 (*iter)->setStatement(std::move(i.fIfFalse));
1282 } else {
1283 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1284 }
1285 }
1286 *outUpdated = true;
1287 *outNeedsRescan = true;
1288 break;
1289 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001290 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1291 // else block doesn't do anything, remove it
1292 i.fIfFalse.reset();
1293 *outUpdated = true;
1294 *outNeedsRescan = true;
1295 }
1296 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1297 // if block doesn't do anything, no else block
1298 if (i.fTest->hasSideEffects()) {
1299 // test has side effects, keep it
1300 (*iter)->setStatement(std::unique_ptr<Statement>(
1301 new ExpressionStatement(std::move(i.fTest))));
1302 } else {
1303 // no if, no else, no test side effects, kill the whole if
1304 // statement
1305 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1306 }
1307 *outUpdated = true;
1308 *outNeedsRescan = true;
1309 }
1310 break;
1311 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001312 case Statement::kSwitch_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001313 SwitchStatement& s = stmt->as<SwitchStatement>();
Brian Osmanb6b95732020-06-30 11:44:27 -04001314 if (s.fValue->isCompileTimeConstant()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 // switch is constant, replace it with the case that matches
1316 bool found = false;
1317 SwitchCase* defaultCase = nullptr;
1318 for (const auto& c : s.fCases) {
1319 if (!c->fValue) {
1320 defaultCase = c.get();
1321 continue;
1322 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001323 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001324 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001325 if (found) {
1326 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1327 if (newBlock) {
1328 (*iter)->setStatement(std::move(newBlock));
1329 break;
1330 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001331 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001332 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001333 "static switch contains non-static conditional break");
1334 s.fIsStatic = false;
1335 }
1336 return; // can't simplify
1337 }
1338 }
1339 }
1340 if (!found) {
1341 // no matching case. use default if it exists, or kill the whole thing
1342 if (defaultCase) {
1343 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1344 if (newBlock) {
1345 (*iter)->setStatement(std::move(newBlock));
1346 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001347 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001348 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001349 "static switch contains non-static conditional break");
1350 s.fIsStatic = false;
1351 }
1352 return; // can't simplify
1353 }
1354 } else {
1355 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1356 }
1357 }
1358 *outUpdated = true;
1359 *outNeedsRescan = true;
1360 }
1361 break;
1362 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001363 case Statement::kExpression_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001364 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001365 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001366 if (!e.fExpression->hasSideEffects()) {
1367 // Expression statement with no side effects, kill it
1368 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1369 *outNeedsRescan = true;
1370 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001371 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001372 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1373 *outUpdated = true;
1374 }
1375 break;
1376 }
1377 default:
1378 break;
1379 }
1380}
1381
1382void Compiler::scanCFG(FunctionDefinition& f) {
1383 CFG cfg = CFGGenerator().getCFG(f);
1384 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001385
1386 // check for unreachable code
1387 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001388 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001389 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001390 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001391 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1392 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001393 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001394 break;
1395 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001396 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001397 if ((*cfg.fBlocks[i].fNodes[0].expression())->fKind ==
1398 Expression::kBoolLiteral_Kind) {
1399 // Function inlining can generate do { ... } while(false) loops which always
1400 // break, so the boolean condition is considered unreachable. Since not
1401 // being able to reach a literal is a non-issue in the first place, we
1402 // don't report an error in this case.
1403 continue;
1404 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001405 break;
1406 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001407 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001408 }
1409 }
1410 if (fErrorCount) {
1411 return;
1412 }
1413
Ethan Nicholascb670962017-04-20 19:31:52 -04001414 // check for dead code & undefined variables, perform constant propagation
1415 std::unordered_set<const Variable*> undefinedVariables;
1416 bool updated;
1417 bool needsRescan = false;
1418 do {
1419 if (needsRescan) {
1420 cfg = CFGGenerator().getCFG(f);
1421 this->computeDataFlow(&cfg);
1422 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001423 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001424
1425 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001426 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001427 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001428 if (!first && b.fEntrances.empty()) {
1429 // Block was reachable before optimization, but has since become unreachable. In
1430 // addition to being dead code, it's broken - since control flow can't reach it, no
1431 // prior variable definitions can reach it, and therefore variables might look to
1432 // have not been properly assigned. Kill it.
1433 for (BasicBlock::Node& node : b.fNodes) {
1434 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
1435 (*node.statement())->fKind != Statement::kNop_Kind) {
1436 node.setStatement(std::unique_ptr<Statement>(new Nop()));
1437 }
1438 }
1439 continue;
1440 }
1441 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001442 DefinitionMap definitions = b.fBefore;
1443
1444 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1445 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1446 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1447 &needsRescan);
1448 } else {
1449 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1450 &needsRescan);
1451 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001452 if (needsRescan) {
1453 break;
1454 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001455 this->addDefinitions(*iter, &definitions);
1456 }
1457 }
1458 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001459 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001460
Ethan Nicholas91a10532017-06-22 11:24:38 -04001461 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001462 for (BasicBlock& b : cfg.fBlocks) {
1463 DefinitionMap definitions = b.fBefore;
1464
Ethan Nicholas91a10532017-06-22 11:24:38 -04001465 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001466 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1467 const Statement& s = **iter->statement();
1468 switch (s.fKind) {
1469 case Statement::kIf_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -04001470 if (s.as<IfStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001471 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001472 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001473 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001474 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001475 break;
1476 case Statement::kSwitch_Kind:
John Stilesa5a97b42020-08-18 11:19:07 -04001477 if (s.as<SwitchStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001478 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001479 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001480 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001481 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001482 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001483 case Statement::kVarDeclarations_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -04001484 VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001485 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1486 if ((*varIter)->fKind == Statement::kNop_Kind) {
1487 varIter = decls.fVars.erase(varIter);
1488 } else {
1489 ++varIter;
1490 }
1491 }
1492 if (!decls.fVars.size()) {
1493 iter = b.fNodes.erase(iter);
1494 } else {
1495 ++iter;
1496 }
1497 break;
1498 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001499 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001500 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001501 break;
1502 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001503 } else {
1504 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001505 }
1506 }
1507 }
1508
ethannicholas22f939e2016-10-13 13:25:34 -07001509 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001510 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001511 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001512 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1513 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001514 }
1515 }
1516}
1517
Ethan Nicholas91164d12019-05-15 15:29:54 -04001518void Compiler::registerExternalValue(ExternalValue* value) {
1519 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1520}
1521
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001522const Symbol* Compiler::takeOwnership(std::unique_ptr<const Symbol> symbol) {
John Stiles3ae071e2020-08-05 15:29:29 -04001523 return fIRGenerator->fRootSymbolTable->takeOwnershipOfSymbol(std::move(symbol));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001524}
1525
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001526std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001527 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001528 fErrorText = "";
1529 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001530 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001531 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001532 switch (kind) {
1533 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001534 inherited = &fVertexInclude;
1535 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001536 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001537 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001538 break;
1539 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001540 inherited = &fFragmentInclude;
1541 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001542 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001543 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001544 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001545 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001546 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001547 inherited = &fGeometryInclude;
1548 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001549 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001550 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001551 break;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001552 case Program::kFragmentProcessor_Kind: {
Brian Osmandd496172020-08-08 08:17:18 -04001553#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001554 {
1555 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1556 SKSL_INCLUDE_sksl_fp,
1557 SKSL_INCLUDE_sksl_fp_LENGTH);
1558 fFPSymbolTable = rehydrator.symbolTable();
1559 fFPInclude = rehydrator.elements();
1560 }
1561 inherited = &fFPInclude;
1562 fIRGenerator->fSymbolTable = fFPSymbolTable;
1563 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
1564 fIRGenerator->start(&settings, inherited);
1565 break;
1566#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001567 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001568 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001569 fIRGenerator->start(&settings, /*inherited=*/nullptr, /*builtin=*/true);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001570 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001571 std::ifstream in(SKSL_FP_INCLUDE);
1572 std::string stdText{std::istreambuf_iterator<char>(in),
1573 std::istreambuf_iterator<char>()};
1574 if (in.rdstate()) {
1575 printf("error reading %s\n", SKSL_FP_INCLUDE);
1576 abort();
1577 }
1578 const String* source = fGpuSymbolTable->takeOwnershipOfString(
1579 std::make_unique<String>(stdText.c_str()));
1580 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001581 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001582 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583#endif
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001584 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001585 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001586 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001587 inherited = &fPipelineInclude;
1588 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001589 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001590 fIRGenerator->start(&settings, inherited);
1591 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001592 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001593 this->loadInterpreterIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001594 inherited = &fInterpreterInclude;
1595 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001596 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001597 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001598 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001599 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001600 std::unique_ptr<String> textPtr(new String(std::move(text)));
1601 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001602 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001603 auto result = std::make_unique<Program>(kind,
1604 std::move(textPtr),
1605 settings,
1606 fContext,
1607 inherited,
1608 std::move(elements),
1609 fIRGenerator->fSymbolTable,
1610 fIRGenerator->fInputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001611 if (fErrorCount) {
1612 return nullptr;
1613 }
1614 return result;
1615}
1616
Ethan Nicholas00543112018-07-31 09:44:36 -04001617bool Compiler::optimize(Program& program) {
1618 SkASSERT(!fErrorCount);
1619 if (!program.fIsOptimized) {
1620 program.fIsOptimized = true;
1621 fIRGenerator->fKind = program.fKind;
1622 fIRGenerator->fSettings = &program.fSettings;
1623 for (auto& element : program) {
1624 if (element.fKind == ProgramElement::kFunction_Kind) {
1625 this->scanCFG((FunctionDefinition&) element);
1626 }
1627 }
Ethan Nicholase8ad02c2020-06-03 16:58:20 -04001628 // we wait until after analysis to remove dead functions so that we still report errors
1629 // even in unused code
1630 if (program.fSettings.fRemoveDeadFunctions) {
1631 for (auto iter = program.fElements.begin(); iter != program.fElements.end(); ) {
1632 if ((*iter)->fKind == ProgramElement::kFunction_Kind) {
1633 const FunctionDefinition& f = (const FunctionDefinition&) **iter;
1634 if (!f.fDeclaration.fCallCount && f.fDeclaration.fName != "main") {
1635 iter = program.fElements.erase(iter);
1636 continue;
1637 }
1638 }
1639 ++iter;
1640 }
1641 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001642 if (program.fKind != Program::kFragmentProcessor_Kind) {
1643 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1644 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1645 VarDeclarations& vars = (VarDeclarations&) **iter;
1646 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1647 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1648 if (var.dead()) {
1649 varIter = vars.fVars.erase(varIter);
1650 } else {
1651 ++varIter;
1652 }
1653 }
1654 if (vars.fVars.size() == 0) {
1655 iter = program.fElements.erase(iter);
1656 continue;
1657 }
1658 }
1659 ++iter;
1660 }
1661 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001662 }
1663 return fErrorCount == 0;
1664}
1665
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001666#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1667
Ethan Nicholas00543112018-07-31 09:44:36 -04001668bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1669 if (!this->optimize(program)) {
1670 return false;
1671 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001672#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001673 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001674 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001675 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001676 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001677 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001678 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001679 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001680 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001681 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001682 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1683 SkDebugf("SPIR-V validation error: %s\n", m);
1684 };
1685 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001686 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001687 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001688 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001689 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001690 }
1691#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001692 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001693 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001694 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001695 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001696#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001697 return result;
1698}
1699
Ethan Nicholas00543112018-07-31 09:44:36 -04001700bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001701 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001702 bool result = this->toSPIRV(program, buffer);
1703 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001704 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001705 }
1706 return result;
1707}
1708
Ethan Nicholas00543112018-07-31 09:44:36 -04001709bool Compiler::toGLSL(Program& program, OutputStream& out) {
1710 if (!this->optimize(program)) {
1711 return false;
1712 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001713 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001714 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001715 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001716 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001717 return result;
1718}
1719
Ethan Nicholas00543112018-07-31 09:44:36 -04001720bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001721 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001722 bool result = this->toGLSL(program, buffer);
1723 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001724 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001725 }
1726 return result;
1727}
1728
Brian Osmanc0243912020-02-19 15:35:26 -05001729bool Compiler::toHLSL(Program& program, String* out) {
1730 String spirv;
1731 if (!this->toSPIRV(program, &spirv)) {
1732 return false;
1733 }
1734
1735 return SPIRVtoHLSL(spirv, out);
1736}
1737
Ethan Nicholas00543112018-07-31 09:44:36 -04001738bool Compiler::toMetal(Program& program, OutputStream& out) {
1739 if (!this->optimize(program)) {
1740 return false;
1741 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001742 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001743 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001744 return result;
1745}
1746
Ethan Nicholas00543112018-07-31 09:44:36 -04001747bool Compiler::toMetal(Program& program, String* out) {
1748 if (!this->optimize(program)) {
1749 return false;
1750 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001751 StringStream buffer;
1752 bool result = this->toMetal(program, buffer);
1753 if (result) {
1754 *out = buffer.str();
1755 }
1756 return result;
1757}
1758
Ethan Nicholasbc6fb272020-08-18 16:29:45 -04001759#if defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS)
Ethan Nicholas00543112018-07-31 09:44:36 -04001760bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1761 if (!this->optimize(program)) {
1762 return false;
1763 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001764 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001765 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001766 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001767 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001768 return result;
1769}
1770
Ethan Nicholas00543112018-07-31 09:44:36 -04001771bool Compiler::toH(Program& program, String name, OutputStream& out) {
1772 if (!this->optimize(program)) {
1773 return false;
1774 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001775 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001776 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001777 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001778 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001779 return result;
1780}
Ethan Nicholasbc6fb272020-08-18 16:29:45 -04001781#endif // defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS)
Ethan Nicholas00543112018-07-31 09:44:36 -04001782
Ethan Nicholasbc6fb272020-08-18 16:29:45 -04001783#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001784
1785#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001786bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
1787 if (!this->optimize(program)) {
1788 return false;
1789 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001790 fSource = program.fSource.get();
1791 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001792 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001793 bool result = cg.generateCode();
1794 fSource = nullptr;
1795 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001796 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001797 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001798 return result;
1799}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001800#endif
1801
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001802std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
1803 if (!this->optimize(program)) {
1804 return nullptr;
1805 }
Brian Osman808f0212020-01-21 15:36:47 -05001806 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001807 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001808 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1809 bool success = cg.generateCode();
1810 fSource = nullptr;
1811 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001812 return result;
1813 }
1814 return nullptr;
1815}
1816
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001817const char* Compiler::OperatorName(Token::Kind kind) {
1818 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001819 case Token::Kind::TK_PLUS: return "+";
1820 case Token::Kind::TK_MINUS: return "-";
1821 case Token::Kind::TK_STAR: return "*";
1822 case Token::Kind::TK_SLASH: return "/";
1823 case Token::Kind::TK_PERCENT: return "%";
1824 case Token::Kind::TK_SHL: return "<<";
1825 case Token::Kind::TK_SHR: return ">>";
1826 case Token::Kind::TK_LOGICALNOT: return "!";
1827 case Token::Kind::TK_LOGICALAND: return "&&";
1828 case Token::Kind::TK_LOGICALOR: return "||";
1829 case Token::Kind::TK_LOGICALXOR: return "^^";
1830 case Token::Kind::TK_BITWISENOT: return "~";
1831 case Token::Kind::TK_BITWISEAND: return "&";
1832 case Token::Kind::TK_BITWISEOR: return "|";
1833 case Token::Kind::TK_BITWISEXOR: return "^";
1834 case Token::Kind::TK_EQ: return "=";
1835 case Token::Kind::TK_EQEQ: return "==";
1836 case Token::Kind::TK_NEQ: return "!=";
1837 case Token::Kind::TK_LT: return "<";
1838 case Token::Kind::TK_GT: return ">";
1839 case Token::Kind::TK_LTEQ: return "<=";
1840 case Token::Kind::TK_GTEQ: return ">=";
1841 case Token::Kind::TK_PLUSEQ: return "+=";
1842 case Token::Kind::TK_MINUSEQ: return "-=";
1843 case Token::Kind::TK_STAREQ: return "*=";
1844 case Token::Kind::TK_SLASHEQ: return "/=";
1845 case Token::Kind::TK_PERCENTEQ: return "%=";
1846 case Token::Kind::TK_SHLEQ: return "<<=";
1847 case Token::Kind::TK_SHREQ: return ">>=";
1848 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1849 case Token::Kind::TK_LOGICALOREQ: return "||=";
1850 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1851 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1852 case Token::Kind::TK_BITWISEOREQ: return "|=";
1853 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1854 case Token::Kind::TK_PLUSPLUS: return "++";
1855 case Token::Kind::TK_MINUSMINUS: return "--";
1856 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001857 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001858 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001859 }
1860}
1861
1862
1863bool Compiler::IsAssignment(Token::Kind op) {
1864 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001865 case Token::Kind::TK_EQ: // fall through
1866 case Token::Kind::TK_PLUSEQ: // fall through
1867 case Token::Kind::TK_MINUSEQ: // fall through
1868 case Token::Kind::TK_STAREQ: // fall through
1869 case Token::Kind::TK_SLASHEQ: // fall through
1870 case Token::Kind::TK_PERCENTEQ: // fall through
1871 case Token::Kind::TK_SHLEQ: // fall through
1872 case Token::Kind::TK_SHREQ: // fall through
1873 case Token::Kind::TK_BITWISEOREQ: // fall through
1874 case Token::Kind::TK_BITWISEXOREQ: // fall through
1875 case Token::Kind::TK_BITWISEANDEQ: // fall through
1876 case Token::Kind::TK_LOGICALOREQ: // fall through
1877 case Token::Kind::TK_LOGICALXOREQ: // fall through
1878 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001879 return true;
1880 default:
1881 return false;
1882 }
1883}
1884
1885Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001886 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001887 int line = 1;
1888 int column = 1;
1889 for (int i = 0; i < offset; i++) {
1890 if ((*fSource)[i] == '\n') {
1891 ++line;
1892 column = 1;
1893 }
1894 else {
1895 ++column;
1896 }
1897 }
1898 return Position(line, column);
1899}
1900
1901void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001902 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001903 Position pos = this->position(offset);
1904 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001905}
1906
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001907String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001908 this->writeErrorCount();
1909 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001910 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001911 return result;
1912}
1913
1914void Compiler::writeErrorCount() {
1915 if (fErrorCount) {
1916 fErrorText += to_string(fErrorCount) + " error";
1917 if (fErrorCount > 1) {
1918 fErrorText += "s";
1919 }
1920 fErrorText += "\n";
1921 }
1922}
1923
John Stilesa6841be2020-08-06 14:11:56 -04001924} // namespace SkSL