blob: 16a24f05c353d428684265e70d08975af3282d53 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLByteCodeGenerator.h"
11#include "src/sksl/SkSLCFGGenerator.h"
12#include "src/sksl/SkSLCPPCodeGenerator.h"
13#include "src/sksl/SkSLGLSLCodeGenerator.h"
14#include "src/sksl/SkSLHCodeGenerator.h"
15#include "src/sksl/SkSLIRGenerator.h"
16#include "src/sksl/SkSLMetalCodeGenerator.h"
17#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
18#include "src/sksl/SkSLSPIRVCodeGenerator.h"
19#include "src/sksl/ir/SkSLEnum.h"
20#include "src/sksl/ir/SkSLExpression.h"
21#include "src/sksl/ir/SkSLExpressionStatement.h"
22#include "src/sksl/ir/SkSLFunctionCall.h"
23#include "src/sksl/ir/SkSLIntLiteral.h"
24#include "src/sksl/ir/SkSLModifiersDeclaration.h"
25#include "src/sksl/ir/SkSLNop.h"
26#include "src/sksl/ir/SkSLSymbolTable.h"
27#include "src/sksl/ir/SkSLTernaryExpression.h"
28#include "src/sksl/ir/SkSLUnresolvedFunction.h"
29#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070030
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040031#ifdef SK_ENABLE_SPIRV_VALIDATION
32#include "spirv-tools/libspirv.hpp"
33#endif
34
ethannicholasb3058bd2016-07-01 08:22:01 -070035// include the built-in shader symbols as static strings
36
Ethan Nicholas79707652017-11-16 11:20:11 -050037#define STRINGIFY(x) #x
38
Ethan Nicholas8da1e652019-05-24 11:01:59 -040039static const char* SKSL_GPU_INCLUDE =
40#include "sksl_gpu.inc"
41;
42
43static const char* SKSL_INTERP_INCLUDE =
44#include "sksl_interp.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070045;
46
ethannicholas5961bc92016-10-12 06:39:56 -070047static const char* SKSL_VERT_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050048#include "sksl_vert.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070049;
50
ethannicholas5961bc92016-10-12 06:39:56 -070051static const char* SKSL_FRAG_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050052#include "sksl_frag.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070053;
54
Ethan Nicholas52cad152017-02-16 16:37:32 -050055static const char* SKSL_GEOM_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050056#include "sksl_geom.inc"
Ethan Nicholas52cad152017-02-16 16:37:32 -050057;
58
Ethan Nicholas762466e2017-06-29 10:03:38 -040059static const char* SKSL_FP_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050060#include "sksl_enums.inc"
61#include "sksl_fp.inc"
Ethan Nicholas762466e2017-06-29 10:03:38 -040062;
63
Ethan Nicholas8da1e652019-05-24 11:01:59 -040064static const char* SKSL_PIPELINE_INCLUDE =
65#include "sksl_pipeline.inc"
Ethan Nicholas0d997662019-04-08 09:46:01 -040066;
67
ethannicholasb3058bd2016-07-01 08:22:01 -070068namespace SkSL {
69
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040070Compiler::Compiler(Flags flags)
71: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040072, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040073, fErrorCount(0) {
Ethan Nicholas8feeff92017-03-30 14:11:58 -040074 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
75 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040076 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070077 fTypes = types;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040078 #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
79 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070080 ADD_TYPE(Void);
81 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040082 ADD_TYPE(Float2);
83 ADD_TYPE(Float3);
84 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040085 ADD_TYPE(Half);
86 ADD_TYPE(Half2);
87 ADD_TYPE(Half3);
88 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -070089 ADD_TYPE(Double);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040090 ADD_TYPE(Double2);
91 ADD_TYPE(Double3);
92 ADD_TYPE(Double4);
ethannicholasb3058bd2016-07-01 08:22:01 -070093 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040094 ADD_TYPE(Int2);
95 ADD_TYPE(Int3);
96 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -070097 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040098 ADD_TYPE(UInt2);
99 ADD_TYPE(UInt3);
100 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400101 ADD_TYPE(Short);
102 ADD_TYPE(Short2);
103 ADD_TYPE(Short3);
104 ADD_TYPE(Short4);
105 ADD_TYPE(UShort);
106 ADD_TYPE(UShort2);
107 ADD_TYPE(UShort3);
108 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400109 ADD_TYPE(Byte);
110 ADD_TYPE(Byte2);
111 ADD_TYPE(Byte3);
112 ADD_TYPE(Byte4);
113 ADD_TYPE(UByte);
114 ADD_TYPE(UByte2);
115 ADD_TYPE(UByte3);
116 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700117 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400118 ADD_TYPE(Bool2);
119 ADD_TYPE(Bool3);
120 ADD_TYPE(Bool4);
121 ADD_TYPE(Float2x2);
122 ADD_TYPE(Float2x3);
123 ADD_TYPE(Float2x4);
124 ADD_TYPE(Float3x2);
125 ADD_TYPE(Float3x3);
126 ADD_TYPE(Float3x4);
127 ADD_TYPE(Float4x2);
128 ADD_TYPE(Float4x3);
129 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400130 ADD_TYPE(Half2x2);
131 ADD_TYPE(Half2x3);
132 ADD_TYPE(Half2x4);
133 ADD_TYPE(Half3x2);
134 ADD_TYPE(Half3x3);
135 ADD_TYPE(Half3x4);
136 ADD_TYPE(Half4x2);
137 ADD_TYPE(Half4x3);
138 ADD_TYPE(Half4x4);
139 ADD_TYPE(Double2x2);
140 ADD_TYPE(Double2x3);
141 ADD_TYPE(Double2x4);
142 ADD_TYPE(Double3x2);
143 ADD_TYPE(Double3x3);
144 ADD_TYPE(Double3x4);
145 ADD_TYPE(Double4x2);
146 ADD_TYPE(Double4x3);
147 ADD_TYPE(Double4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400149 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 ADD_TYPE(GenDType);
151 ADD_TYPE(GenIType);
152 ADD_TYPE(GenUType);
153 ADD_TYPE(GenBType);
154 ADD_TYPE(Mat);
155 ADD_TYPE(Vec);
156 ADD_TYPE(GVec);
157 ADD_TYPE(GVec2);
158 ADD_TYPE(GVec3);
159 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400160 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700161 ADD_TYPE(DVec);
162 ADD_TYPE(IVec);
163 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400164 ADD_TYPE(SVec);
165 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400166 ADD_TYPE(ByteVec);
167 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700168 ADD_TYPE(BVec);
169
170 ADD_TYPE(Sampler1D);
171 ADD_TYPE(Sampler2D);
172 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700173 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700174 ADD_TYPE(SamplerCube);
175 ADD_TYPE(Sampler2DRect);
176 ADD_TYPE(Sampler1DArray);
177 ADD_TYPE(Sampler2DArray);
178 ADD_TYPE(SamplerCubeArray);
179 ADD_TYPE(SamplerBuffer);
180 ADD_TYPE(Sampler2DMS);
181 ADD_TYPE(Sampler2DMSArray);
182
Brian Salomonbf7b6202016-11-11 16:08:03 -0500183 ADD_TYPE(ISampler2D);
184
Brian Salomon2a51de82016-11-16 12:06:01 -0500185 ADD_TYPE(Image2D);
186 ADD_TYPE(IImage2D);
187
Greg Daniel64773e62016-11-22 09:44:03 -0500188 ADD_TYPE(SubpassInput);
189 ADD_TYPE(SubpassInputMS);
190
ethannicholasb3058bd2016-07-01 08:22:01 -0700191 ADD_TYPE(GSampler1D);
192 ADD_TYPE(GSampler2D);
193 ADD_TYPE(GSampler3D);
194 ADD_TYPE(GSamplerCube);
195 ADD_TYPE(GSampler2DRect);
196 ADD_TYPE(GSampler1DArray);
197 ADD_TYPE(GSampler2DArray);
198 ADD_TYPE(GSamplerCubeArray);
199 ADD_TYPE(GSamplerBuffer);
200 ADD_TYPE(GSampler2DMS);
201 ADD_TYPE(GSampler2DMSArray);
202
203 ADD_TYPE(Sampler1DShadow);
204 ADD_TYPE(Sampler2DShadow);
205 ADD_TYPE(SamplerCubeShadow);
206 ADD_TYPE(Sampler2DRectShadow);
207 ADD_TYPE(Sampler1DArrayShadow);
208 ADD_TYPE(Sampler2DArrayShadow);
209 ADD_TYPE(SamplerCubeArrayShadow);
210 ADD_TYPE(GSampler2DArrayShadow);
211 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400212 ADD_TYPE(FragmentProcessor);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400213 ADD_TYPE(SkRasterPipeline);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400214 ADD_TYPE(Sampler);
215 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700216
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700217 StringFragment skCapsName("sk_Caps");
218 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400219 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500220 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
221
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700222 StringFragment skArgsName("sk_Args");
223 Variable* skArgs = new Variable(-1, Modifiers(), skArgsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400224 *fContext->fSkArgs_Type, Variable::kGlobal_Storage);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400225 fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs));
226
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400227 std::vector<std::unique_ptr<ProgramElement>> ignored;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400228 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
229 symbols, &ignored, &fGpuSymbolTable);
230 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
231 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
232 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
233 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
234 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, strlen(SKSL_GEOM_INCLUDE),
235 fGpuSymbolTable, &fGeometryInclude, &fGeometrySymbolTable);
236 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
237 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
238 &fPipelineSymbolTable);
239 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
240 strlen(SKSL_INTERP_INCLUDE), symbols, &fInterpreterInclude,
241 &fInterpreterSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -0700242}
243
244Compiler::~Compiler() {
245 delete fIRGenerator;
246}
247
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400248void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
249 std::shared_ptr<SymbolTable> base,
250 std::vector<std::unique_ptr<ProgramElement>>* outElements,
251 std::shared_ptr<SymbolTable>* outSymbolTable) {
252 fIRGenerator->fSymbolTable = std::move(base);
253 Program::Settings settings;
254 fIRGenerator->start(&settings, nullptr);
255 fIRGenerator->convertProgram(kind, src, length, *fTypes, outElements);
256 if (this->fErrorCount) {
257 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
258 }
259 SkASSERT(!fErrorCount);
260 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
261 *outSymbolTable = fIRGenerator->fSymbolTable;
262}
263
ethannicholas22f939e2016-10-13 13:25:34 -0700264// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500265void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
266 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700267 switch (lvalue->fKind) {
268 case Expression::kVariableReference_Kind: {
269 const Variable& var = ((VariableReference*) lvalue)->fVariable;
270 if (var.fStorage == Variable::kLocal_Storage) {
271 (*definitions)[&var] = expr;
272 }
273 break;
274 }
275 case Expression::kSwizzle_Kind:
276 // We consider the variable written to as long as at least some of its components have
277 // been written to. This will lead to some false negatives (we won't catch it if you
278 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400279 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
280 // 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 -0700281 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400282 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400283 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700284 definitions);
285 break;
286 case Expression::kIndex_Kind:
287 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400288 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400289 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700290 definitions);
291 break;
292 case Expression::kFieldAccess_Kind:
293 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400294 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400295 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700296 definitions);
297 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500298 case Expression::kTernary_Kind:
299 // To simplify analysis, we just pretend that we write to both sides of the ternary.
300 // This allows for false positives (meaning we fail to detect that a variable might not
301 // have been assigned), but is preferable to false negatives.
302 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400303 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500304 definitions);
305 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400306 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500307 definitions);
308 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400309 case Expression::kExternalValue_Kind:
310 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700311 default:
312 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400313 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700314 }
315}
316
317// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400318void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500319 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700320 switch (node.fKind) {
321 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400322 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400323 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500324 switch (expr->fKind) {
325 case Expression::kBinary_Kind: {
326 BinaryExpression* b = (BinaryExpression*) expr;
327 if (b->fOperator == Token::EQ) {
328 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700329 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500330 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400331 b->fLeft.get(),
332 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
333 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500334
335 }
336 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700337 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400338 case Expression::kFunctionCall_Kind: {
339 const FunctionCall& c = (const FunctionCall&) *expr;
340 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
341 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
342 this->addDefinition(
343 c.fArguments[i].get(),
344 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
345 definitions);
346 }
347 }
348 break;
349 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500350 case Expression::kPrefix_Kind: {
351 const PrefixExpression* p = (PrefixExpression*) expr;
352 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
353 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400354 p->fOperand.get(),
355 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
356 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500357 }
358 break;
359 }
360 case Expression::kPostfix_Kind: {
361 const PostfixExpression* p = (PostfixExpression*) expr;
362 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
363 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400364 p->fOperand.get(),
365 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
366 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500367 }
368 break;
369 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400370 case Expression::kVariableReference_Kind: {
371 const VariableReference* v = (VariableReference*) expr;
372 if (v->fRefKind != VariableReference::kRead_RefKind) {
373 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400374 v,
375 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
376 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400377 }
378 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500379 default:
380 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700381 }
382 break;
383 }
384 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400385 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000386 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
387 VarDeclaration& vd = (VarDeclaration&) *stmt;
388 if (vd.fValue) {
389 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700390 }
391 }
392 break;
393 }
394 }
395}
396
397void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
398 BasicBlock& block = cfg->fBlocks[blockId];
399
400 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500401 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700402 for (const BasicBlock::Node& n : block.fNodes) {
403 this->addDefinitions(n, &after);
404 }
405
406 // propagate definitions to exits
407 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400408 if (exitId == blockId) {
409 continue;
410 }
ethannicholas22f939e2016-10-13 13:25:34 -0700411 BasicBlock& exit = cfg->fBlocks[exitId];
412 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500413 std::unique_ptr<Expression>* e1 = pair.second;
414 auto found = exit.fBefore.find(pair.first);
415 if (found == exit.fBefore.end()) {
416 // exit has no definition for it, just copy it
417 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700418 exit.fBefore[pair.first] = e1;
419 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500420 // exit has a (possibly different) value already defined
421 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700422 if (e1 != e2) {
423 // definition has changed, merge and add exit block to worklist
424 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500425 if (e1 && e2) {
426 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400427 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500428 } else {
429 exit.fBefore[pair.first] = nullptr;
430 }
ethannicholas22f939e2016-10-13 13:25:34 -0700431 }
432 }
433 }
434 }
435}
436
437// returns a map which maps all local variables in the function to null, indicating that their value
438// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500439static DefinitionMap compute_start_state(const CFG& cfg) {
440 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400441 for (const auto& block : cfg.fBlocks) {
442 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700443 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400444 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400445 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700446 if (s->fKind == Statement::kVarDeclarations_Kind) {
447 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000448 for (const auto& decl : vd->fDeclaration->fVars) {
449 if (decl->fKind == Statement::kVarDeclaration_Kind) {
450 result[((VarDeclaration&) *decl).fVar] = nullptr;
451 }
Mike Klein6ad99092016-10-26 10:35:22 -0400452 }
ethannicholas22f939e2016-10-13 13:25:34 -0700453 }
454 }
455 }
456 }
457 return result;
458}
459
Ethan Nicholascb670962017-04-20 19:31:52 -0400460/**
461 * Returns true if assigning to this lvalue has no effect.
462 */
463static bool is_dead(const Expression& lvalue) {
464 switch (lvalue.fKind) {
465 case Expression::kVariableReference_Kind:
466 return ((VariableReference&) lvalue).fVariable.dead();
467 case Expression::kSwizzle_Kind:
468 return is_dead(*((Swizzle&) lvalue).fBase);
469 case Expression::kFieldAccess_Kind:
470 return is_dead(*((FieldAccess&) lvalue).fBase);
471 case Expression::kIndex_Kind: {
472 const IndexExpression& idx = (IndexExpression&) lvalue;
473 return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects();
474 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500475 case Expression::kTernary_Kind: {
476 const TernaryExpression& t = (TernaryExpression&) lvalue;
477 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
478 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400479 case Expression::kExternalValue_Kind:
480 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400481 default:
482 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
483 }
484}
ethannicholas22f939e2016-10-13 13:25:34 -0700485
Ethan Nicholascb670962017-04-20 19:31:52 -0400486/**
487 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
488 * to a dead target and lack of side effects on the left hand side.
489 */
490static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700491 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400492 return false;
493 }
494 return is_dead(*b.fLeft);
495}
496
497void Compiler::computeDataFlow(CFG* cfg) {
498 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700499 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400500 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700501 workList.insert(i);
502 }
503 while (workList.size()) {
504 BlockId next = *workList.begin();
505 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400506 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700507 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400508}
509
510/**
511 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
512 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
513 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
514 * need to be regenerated).
515 */
516bool try_replace_expression(BasicBlock* b,
517 std::vector<BasicBlock::Node>::iterator* iter,
518 std::unique_ptr<Expression>* newExpression) {
519 std::unique_ptr<Expression>* target = (*iter)->expression();
520 if (!b->tryRemoveExpression(iter)) {
521 *target = std::move(*newExpression);
522 return false;
523 }
524 *target = std::move(*newExpression);
525 return b->tryInsertExpression(iter, target);
526}
527
528/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400529 * Returns true if the expression is a constant numeric literal with the specified value, or a
530 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400531 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400532bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400533 switch (expr.fKind) {
534 case Expression::kIntLiteral_Kind:
535 return ((IntLiteral&) expr).fValue == value;
536 case Expression::kFloatLiteral_Kind:
537 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400538 case Expression::kConstructor_Kind: {
539 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400540 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
541 : c.fType.isFloat();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400542 if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) {
543 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400544 if (isFloat) {
545 if (c.getFVecComponent(i) != value) {
546 return false;
547 }
548 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400549 return false;
550 }
551 }
552 return true;
553 }
554 return false;
555 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400556 default:
557 return false;
558 }
559}
560
561/**
562 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
563 * and CFG structures).
564 */
565void delete_left(BasicBlock* b,
566 std::vector<BasicBlock::Node>::iterator* iter,
567 bool* outUpdated,
568 bool* outNeedsRescan) {
569 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400570 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400571 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400572 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400573 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400574 bool result;
575 if (bin.fOperator == Token::EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400576 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400577 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400578 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400579 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400580 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400581 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400582 *outNeedsRescan = true;
583 return;
584 }
585 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400586 *outNeedsRescan = true;
587 return;
588 }
589 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400590 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
591 (*iter)->expression() != &bin.fRight) {
592 *outNeedsRescan = true;
593 return;
594 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400595 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400596 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400597}
598
599/**
600 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
601 * CFG structures).
602 */
603void delete_right(BasicBlock* b,
604 std::vector<BasicBlock::Node>::iterator* iter,
605 bool* outUpdated,
606 bool* outNeedsRescan) {
607 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400608 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400609 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400610 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400611 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400612 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
613 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400614 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400615 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400616 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400617 *target = std::move(bin.fLeft);
618 if (*iter == b->fNodes.begin()) {
619 *outNeedsRescan = true;
620 return;
621 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400622 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400623 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
624 (*iter)->expression() != &bin.fLeft)) {
625 *outNeedsRescan = true;
626 return;
627 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400628 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400629 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400630}
631
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400632/**
633 * Constructs the specified type using a single argument.
634 */
635static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
636 std::vector<std::unique_ptr<Expression>> args;
637 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700638 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400639 return result;
640}
641
642/**
643 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
644 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
645 */
646static void vectorize(BasicBlock* b,
647 std::vector<BasicBlock::Node>::iterator* iter,
648 const Type& type,
649 std::unique_ptr<Expression>* otherExpression,
650 bool* outUpdated,
651 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400652 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
653 SkASSERT(type.kind() == Type::kVector_Kind);
654 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400655 *outUpdated = true;
656 std::unique_ptr<Expression>* target = (*iter)->expression();
657 if (!b->tryRemoveExpression(iter)) {
658 *target = construct(type, std::move(*otherExpression));
659 *outNeedsRescan = true;
660 } else {
661 *target = construct(type, std::move(*otherExpression));
662 if (!b->tryInsertExpression(iter, target)) {
663 *outNeedsRescan = true;
664 }
665 }
666}
667
668/**
669 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
670 * left to yield vec<n>(x).
671 */
672static void vectorize_left(BasicBlock* b,
673 std::vector<BasicBlock::Node>::iterator* iter,
674 bool* outUpdated,
675 bool* outNeedsRescan) {
676 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
677 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
678}
679
680/**
681 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
682 * right to yield vec<n>(y).
683 */
684static void vectorize_right(BasicBlock* b,
685 std::vector<BasicBlock::Node>::iterator* iter,
686 bool* outUpdated,
687 bool* outNeedsRescan) {
688 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
689 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
690}
691
692// Mark that an expression which we were writing to is no longer being written to
693void clear_write(const Expression& expr) {
694 switch (expr.fKind) {
695 case Expression::kVariableReference_Kind: {
696 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
697 break;
698 }
699 case Expression::kFieldAccess_Kind:
700 clear_write(*((FieldAccess&) expr).fBase);
701 break;
702 case Expression::kSwizzle_Kind:
703 clear_write(*((Swizzle&) expr).fBase);
704 break;
705 case Expression::kIndex_Kind:
706 clear_write(*((IndexExpression&) expr).fBase);
707 break;
708 default:
709 ABORT("shouldn't be writing to this kind of expression\n");
710 break;
711 }
712}
713
Ethan Nicholascb670962017-04-20 19:31:52 -0400714void Compiler::simplifyExpression(DefinitionMap& definitions,
715 BasicBlock& b,
716 std::vector<BasicBlock::Node>::iterator* iter,
717 std::unordered_set<const Variable*>* undefinedVariables,
718 bool* outUpdated,
719 bool* outNeedsRescan) {
720 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400721 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400722 if ((*iter)->fConstantPropagation) {
723 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
724 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400725 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400726 if (!try_replace_expression(&b, iter, &optimized)) {
727 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400728 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400729 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400730 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400731 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400732 }
733 }
734 switch (expr->fKind) {
735 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400736 const VariableReference& ref = (VariableReference&) *expr;
737 const Variable& var = ref.fVariable;
738 if (ref.refKind() != VariableReference::kWrite_RefKind &&
739 ref.refKind() != VariableReference::kPointer_RefKind &&
740 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400741 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
742 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000743 this->error(expr->fOffset,
744 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400745 }
746 break;
747 }
748 case Expression::kTernary_Kind: {
749 TernaryExpression* t = (TernaryExpression*) expr;
750 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
751 // ternary has a constant test, replace it with either the true or
752 // false branch
753 if (((BoolLiteral&) *t->fTest).fValue) {
754 (*iter)->setExpression(std::move(t->fIfTrue));
755 } else {
756 (*iter)->setExpression(std::move(t->fIfFalse));
757 }
758 *outUpdated = true;
759 *outNeedsRescan = true;
760 }
761 break;
762 }
763 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400764 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400765 if (dead_assignment(*bin)) {
766 delete_left(&b, iter, outUpdated, outNeedsRescan);
767 break;
768 }
769 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400770 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
771 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
772 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
773 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
774 break;
775 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400776 switch (bin->fOperator) {
777 case Token::STAR:
778 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400779 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
780 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400781 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400782 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
783 } else {
784 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400785 // 1 * float4(x) -> float4(x)
786 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400787 delete_left(&b, iter, outUpdated, outNeedsRescan);
788 }
789 }
790 else if (is_constant(*bin->fLeft, 0)) {
791 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500792 bin->fRight->fType.kind() == Type::kVector_Kind &&
793 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400794 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400795 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
796 } else {
797 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400798 // float4(0) * x -> float4(0)
799 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500800 if (!bin->fRight->hasSideEffects()) {
801 delete_right(&b, iter, outUpdated, outNeedsRescan);
802 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400803 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400804 }
805 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400806 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
807 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400808 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400809 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
810 } else {
811 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400812 // float4(x) * 1 -> float4(x)
813 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400814 delete_right(&b, iter, outUpdated, outNeedsRescan);
815 }
816 }
817 else if (is_constant(*bin->fRight, 0)) {
818 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500819 bin->fRight->fType.kind() == Type::kScalar_Kind &&
820 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400821 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400822 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
823 } else {
824 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400825 // x * float4(0) -> float4(0)
826 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500827 if (!bin->fLeft->hasSideEffects()) {
828 delete_left(&b, iter, outUpdated, outNeedsRescan);
829 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400830 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400831 }
832 break;
Ethan Nicholas56e42712017-04-21 10:23:37 -0400833 case Token::PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400834 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400835 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
836 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400837 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400838 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
839 } else {
840 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400841 // 0 + float4(x) -> float4(x)
842 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400843 delete_left(&b, iter, outUpdated, outNeedsRescan);
844 }
845 } else if (is_constant(*bin->fRight, 0)) {
846 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
847 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400848 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400849 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
850 } else {
851 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400852 // float4(x) + 0 -> float4(x)
853 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400854 delete_right(&b, iter, outUpdated, outNeedsRescan);
855 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400856 }
857 break;
858 case Token::MINUS:
859 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400860 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
861 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400862 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400863 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
864 } else {
865 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400866 // float4(x) - 0 -> float4(x)
867 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400868 delete_right(&b, iter, outUpdated, outNeedsRescan);
869 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400870 }
871 break;
872 case Token::SLASH:
873 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400874 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
875 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400876 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400877 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
878 } else {
879 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400880 // float4(x) / 1 -> float4(x)
881 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400882 delete_right(&b, iter, outUpdated, outNeedsRescan);
883 }
884 } else if (is_constant(*bin->fLeft, 0)) {
885 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500886 bin->fRight->fType.kind() == Type::kVector_Kind &&
887 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400888 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400889 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
890 } else {
891 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400892 // float4(0) / x -> float4(0)
893 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500894 if (!bin->fRight->hasSideEffects()) {
895 delete_right(&b, iter, outUpdated, outNeedsRescan);
896 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400897 }
898 }
899 break;
900 case Token::PLUSEQ:
901 if (is_constant(*bin->fRight, 0)) {
902 clear_write(*bin->fLeft);
903 delete_right(&b, iter, outUpdated, outNeedsRescan);
904 }
905 break;
906 case Token::MINUSEQ:
907 if (is_constant(*bin->fRight, 0)) {
908 clear_write(*bin->fLeft);
909 delete_right(&b, iter, outUpdated, outNeedsRescan);
910 }
911 break;
912 case Token::STAREQ:
913 if (is_constant(*bin->fRight, 1)) {
914 clear_write(*bin->fLeft);
915 delete_right(&b, iter, outUpdated, outNeedsRescan);
916 }
917 break;
918 case Token::SLASHEQ:
919 if (is_constant(*bin->fRight, 1)) {
920 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400921 delete_right(&b, iter, outUpdated, outNeedsRescan);
922 }
923 break;
924 default:
925 break;
926 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -0400927 break;
928 }
929 case Expression::kSwizzle_Kind: {
930 Swizzle& s = (Swizzle&) *expr;
931 // detect identity swizzles like foo.rgba
932 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
933 bool identity = true;
934 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
935 if (s.fComponents[i] != i) {
936 identity = false;
937 break;
938 }
939 }
940 if (identity) {
941 *outUpdated = true;
942 if (!try_replace_expression(&b, iter, &s.fBase)) {
943 *outNeedsRescan = true;
944 return;
945 }
946 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
947 break;
948 }
949 }
950 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
951 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
952 Swizzle& base = (Swizzle&) *s.fBase;
953 std::vector<int> final;
954 for (int c : s.fComponents) {
955 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
956 final.push_back(c);
957 } else {
958 final.push_back(base.fComponents[c]);
959 }
960 }
961 *outUpdated = true;
962 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
963 std::move(final)));
964 if (!try_replace_expression(&b, iter, &replacement)) {
965 *outNeedsRescan = true;
966 return;
967 }
968 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
969 break;
970 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400971 }
972 default:
973 break;
974 }
975}
976
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400977// returns true if this statement could potentially execute a break at the current level (we ignore
978// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
Ethan Nicholas5005a222018-08-24 13:06:27 -0400979static bool contains_conditional_break(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400980 switch (s.fKind) {
981 case Statement::kBlock_Kind:
982 for (const auto& sub : ((Block&) s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -0400983 if (contains_conditional_break(*sub, inConditional)) {
984 return true;
985 }
986 }
987 return false;
988 case Statement::kBreak_Kind:
989 return inConditional;
990 case Statement::kIf_Kind: {
991 const IfStatement& i = (IfStatement&) s;
992 return contains_conditional_break(*i.fIfTrue, true) ||
993 (i.fIfFalse && contains_conditional_break(*i.fIfFalse, true));
994 }
995 default:
996 return false;
997 }
998}
999
1000// returns true if this statement definitely executes a break at the current level (we ignore
1001// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1002static bool contains_unconditional_break(Statement& s) {
1003 switch (s.fKind) {
1004 case Statement::kBlock_Kind:
1005 for (const auto& sub : ((Block&) s).fStatements) {
1006 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001007 return true;
1008 }
1009 }
1010 return false;
1011 case Statement::kBreak_Kind:
1012 return true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001013 default:
1014 return false;
1015 }
1016}
1017
1018// Returns a block containing all of the statements that will be run if the given case matches
1019// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1020// broken by this call and must then be discarded).
1021// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1022// when break statements appear inside conditionals.
1023static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) {
1024 bool capturing = false;
1025 std::vector<std::unique_ptr<Statement>*> statementPtrs;
1026 for (const auto& current : s->fCases) {
1027 if (current.get() == c) {
1028 capturing = true;
1029 }
1030 if (capturing) {
1031 for (auto& stmt : current->fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001032 if (contains_conditional_break(*stmt, s->fKind == Statement::kIf_Kind)) {
1033 return nullptr;
1034 }
1035 if (contains_unconditional_break(*stmt)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001036 capturing = false;
1037 break;
1038 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001039 statementPtrs.push_back(&stmt);
1040 }
1041 if (!capturing) {
1042 break;
1043 }
1044 }
1045 }
1046 std::vector<std::unique_ptr<Statement>> statements;
1047 for (const auto& s : statementPtrs) {
1048 statements.push_back(std::move(*s));
1049 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001050 return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001051}
1052
Ethan Nicholascb670962017-04-20 19:31:52 -04001053void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001054 BasicBlock& b,
1055 std::vector<BasicBlock::Node>::iterator* iter,
1056 std::unordered_set<const Variable*>* undefinedVariables,
1057 bool* outUpdated,
1058 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001059 Statement* stmt = (*iter)->statement()->get();
1060 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001061 case Statement::kVarDeclaration_Kind: {
1062 const auto& varDecl = (VarDeclaration&) *stmt;
1063 if (varDecl.fVar->dead() &&
1064 (!varDecl.fValue ||
1065 !varDecl.fValue->hasSideEffects())) {
1066 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001067 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001068 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1069 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001070 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001071 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001072 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001073 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001074 }
1075 break;
1076 }
1077 case Statement::kIf_Kind: {
1078 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001079 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1080 // constant if, collapse down to a single branch
1081 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001082 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001083 (*iter)->setStatement(std::move(i.fIfTrue));
1084 } else {
1085 if (i.fIfFalse) {
1086 (*iter)->setStatement(std::move(i.fIfFalse));
1087 } else {
1088 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1089 }
1090 }
1091 *outUpdated = true;
1092 *outNeedsRescan = true;
1093 break;
1094 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001095 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1096 // else block doesn't do anything, remove it
1097 i.fIfFalse.reset();
1098 *outUpdated = true;
1099 *outNeedsRescan = true;
1100 }
1101 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1102 // if block doesn't do anything, no else block
1103 if (i.fTest->hasSideEffects()) {
1104 // test has side effects, keep it
1105 (*iter)->setStatement(std::unique_ptr<Statement>(
1106 new ExpressionStatement(std::move(i.fTest))));
1107 } else {
1108 // no if, no else, no test side effects, kill the whole if
1109 // statement
1110 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1111 }
1112 *outUpdated = true;
1113 *outNeedsRescan = true;
1114 }
1115 break;
1116 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001117 case Statement::kSwitch_Kind: {
1118 SwitchStatement& s = (SwitchStatement&) *stmt;
1119 if (s.fValue->isConstant()) {
1120 // switch is constant, replace it with the case that matches
1121 bool found = false;
1122 SwitchCase* defaultCase = nullptr;
1123 for (const auto& c : s.fCases) {
1124 if (!c->fValue) {
1125 defaultCase = c.get();
1126 continue;
1127 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001128 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001129 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001130 if (found) {
1131 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1132 if (newBlock) {
1133 (*iter)->setStatement(std::move(newBlock));
1134 break;
1135 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001136 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001137 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001138 "static switch contains non-static conditional break");
1139 s.fIsStatic = false;
1140 }
1141 return; // can't simplify
1142 }
1143 }
1144 }
1145 if (!found) {
1146 // no matching case. use default if it exists, or kill the whole thing
1147 if (defaultCase) {
1148 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1149 if (newBlock) {
1150 (*iter)->setStatement(std::move(newBlock));
1151 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001152 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001153 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001154 "static switch contains non-static conditional break");
1155 s.fIsStatic = false;
1156 }
1157 return; // can't simplify
1158 }
1159 } else {
1160 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1161 }
1162 }
1163 *outUpdated = true;
1164 *outNeedsRescan = true;
1165 }
1166 break;
1167 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001168 case Statement::kExpression_Kind: {
1169 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001170 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001171 if (!e.fExpression->hasSideEffects()) {
1172 // Expression statement with no side effects, kill it
1173 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1174 *outNeedsRescan = true;
1175 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001176 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001177 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1178 *outUpdated = true;
1179 }
1180 break;
1181 }
1182 default:
1183 break;
1184 }
1185}
1186
1187void Compiler::scanCFG(FunctionDefinition& f) {
1188 CFG cfg = CFGGenerator().getCFG(f);
1189 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001190
1191 // check for unreachable code
1192 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001193 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001194 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001195 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001196 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1197 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001198 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001199 break;
1200 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001201 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001202 break;
1203 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001204 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001205 }
1206 }
1207 if (fErrorCount) {
1208 return;
1209 }
1210
Ethan Nicholascb670962017-04-20 19:31:52 -04001211 // check for dead code & undefined variables, perform constant propagation
1212 std::unordered_set<const Variable*> undefinedVariables;
1213 bool updated;
1214 bool needsRescan = false;
1215 do {
1216 if (needsRescan) {
1217 cfg = CFGGenerator().getCFG(f);
1218 this->computeDataFlow(&cfg);
1219 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001220 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001221
1222 updated = false;
1223 for (BasicBlock& b : cfg.fBlocks) {
1224 DefinitionMap definitions = b.fBefore;
1225
1226 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1227 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1228 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1229 &needsRescan);
1230 } else {
1231 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1232 &needsRescan);
1233 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001234 if (needsRescan) {
1235 break;
1236 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001237 this->addDefinitions(*iter, &definitions);
1238 }
1239 }
1240 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001241 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001242
Ethan Nicholas91a10532017-06-22 11:24:38 -04001243 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001244 for (BasicBlock& b : cfg.fBlocks) {
1245 DefinitionMap definitions = b.fBefore;
1246
Ethan Nicholas91a10532017-06-22 11:24:38 -04001247 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001248 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1249 const Statement& s = **iter->statement();
1250 switch (s.fKind) {
1251 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001252 if (((const IfStatement&) s).fIsStatic &&
1253 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001254 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001255 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001256 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001257 break;
1258 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001259 if (((const SwitchStatement&) s).fIsStatic &&
1260 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001261 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001262 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001263 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001264 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001265 case Statement::kVarDeclarations_Kind: {
1266 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1267 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1268 if ((*varIter)->fKind == Statement::kNop_Kind) {
1269 varIter = decls.fVars.erase(varIter);
1270 } else {
1271 ++varIter;
1272 }
1273 }
1274 if (!decls.fVars.size()) {
1275 iter = b.fNodes.erase(iter);
1276 } else {
1277 ++iter;
1278 }
1279 break;
1280 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001281 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001282 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001283 break;
1284 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001285 } else {
1286 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001287 }
1288 }
1289 }
1290
ethannicholas22f939e2016-10-13 13:25:34 -07001291 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001292 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001293 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001294 this->error(f.fOffset, String("function can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001295 }
1296 }
1297}
1298
Ethan Nicholas91164d12019-05-15 15:29:54 -04001299void Compiler::registerExternalValue(ExternalValue* value) {
1300 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1301}
1302
1303Symbol* Compiler::takeOwnership(std::unique_ptr<Symbol> symbol) {
1304 return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol));
1305}
1306
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001307std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001308 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001309 fErrorText = "";
1310 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001311 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001312 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001313 switch (kind) {
1314 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001315 inherited = &fVertexInclude;
1316 fIRGenerator->fSymbolTable = fVertexSymbolTable;
1317 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001318 break;
1319 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001320 inherited = &fFragmentInclude;
1321 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
1322 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001323 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001324 case Program::kGeometry_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001325 inherited = &fGeometryInclude;
1326 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
1327 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001328 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001329 case Program::kFragmentProcessor_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001330 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001331 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001332 fIRGenerator->start(&settings, nullptr);
Robert Phillipsfe8da172018-01-24 14:52:02 +00001333 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes,
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001334 &elements);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001335 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001336 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001337 case Program::kPipelineStage_Kind:
1338 inherited = &fPipelineInclude;
1339 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
1340 fIRGenerator->start(&settings, inherited);
1341 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001342 case Program::kGeneric_Kind:
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001343 inherited = &fInterpreterInclude;
1344 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
1345 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001346 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001347 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001348 for (auto& element : elements) {
1349 if (element->fKind == ProgramElement::kEnum_Kind) {
1350 ((Enum&) *element).fBuiltin = true;
1351 }
1352 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001353 std::unique_ptr<String> textPtr(new String(std::move(text)));
1354 fSource = textPtr.get();
Robert Phillipsfe8da172018-01-24 14:52:02 +00001355 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001356 auto result = std::unique_ptr<Program>(new Program(kind,
1357 std::move(textPtr),
1358 settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001359 fContext,
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001360 inherited,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001361 std::move(elements),
1362 fIRGenerator->fSymbolTable,
1363 fIRGenerator->fInputs));
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001364 if (fErrorCount) {
1365 return nullptr;
1366 }
1367 return result;
1368}
1369
Ethan Nicholas00543112018-07-31 09:44:36 -04001370bool Compiler::optimize(Program& program) {
1371 SkASSERT(!fErrorCount);
1372 if (!program.fIsOptimized) {
1373 program.fIsOptimized = true;
1374 fIRGenerator->fKind = program.fKind;
1375 fIRGenerator->fSettings = &program.fSettings;
1376 for (auto& element : program) {
1377 if (element.fKind == ProgramElement::kFunction_Kind) {
1378 this->scanCFG((FunctionDefinition&) element);
1379 }
1380 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001381 if (program.fKind != Program::kFragmentProcessor_Kind) {
1382 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1383 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1384 VarDeclarations& vars = (VarDeclarations&) **iter;
1385 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1386 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1387 if (var.dead()) {
1388 varIter = vars.fVars.erase(varIter);
1389 } else {
1390 ++varIter;
1391 }
1392 }
1393 if (vars.fVars.size() == 0) {
1394 iter = program.fElements.erase(iter);
1395 continue;
1396 }
1397 }
1398 ++iter;
1399 }
1400 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001401 }
1402 return fErrorCount == 0;
1403}
1404
1405std::unique_ptr<Program> Compiler::specialize(
1406 Program& program,
1407 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1408 std::vector<std::unique_ptr<ProgramElement>> elements;
1409 for (const auto& e : program) {
1410 elements.push_back(e.clone());
1411 }
1412 Program::Settings settings;
1413 settings.fCaps = program.fSettings.fCaps;
1414 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1415 settings.fArgs.insert(*iter);
1416 }
1417 std::unique_ptr<Program> result(new Program(program.fKind,
1418 nullptr,
1419 settings,
1420 program.fContext,
1421 program.fInheritedElements,
1422 std::move(elements),
1423 program.fSymbols,
1424 program.fInputs));
1425 return result;
1426}
1427
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001428#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1429
Ethan Nicholas00543112018-07-31 09:44:36 -04001430bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1431 if (!this->optimize(program)) {
1432 return false;
1433 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001434#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001435 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001436 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001437 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001438 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001439 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001440 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001441 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001442 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001443 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001444 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1445 SkDebugf("SPIR-V validation error: %s\n", m);
1446 };
1447 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001448 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001449 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001450 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001451 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001452 }
1453#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001454 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001455 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001456 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001457 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001458#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001459 return result;
1460}
1461
Ethan Nicholas00543112018-07-31 09:44:36 -04001462bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001463 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001464 bool result = this->toSPIRV(program, buffer);
1465 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001466 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001467 }
1468 return result;
1469}
1470
Ethan Nicholas00543112018-07-31 09:44:36 -04001471bool Compiler::toGLSL(Program& program, OutputStream& out) {
1472 if (!this->optimize(program)) {
1473 return false;
1474 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001475 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001476 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001477 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001478 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001479 return result;
1480}
1481
Ethan Nicholas00543112018-07-31 09:44:36 -04001482bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001483 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001484 bool result = this->toGLSL(program, buffer);
1485 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001486 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001487 }
1488 return result;
1489}
1490
Ethan Nicholas00543112018-07-31 09:44:36 -04001491bool Compiler::toMetal(Program& program, OutputStream& out) {
1492 if (!this->optimize(program)) {
1493 return false;
1494 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001495 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001496 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001497 return result;
1498}
1499
Ethan Nicholas00543112018-07-31 09:44:36 -04001500bool Compiler::toMetal(Program& program, String* out) {
1501 if (!this->optimize(program)) {
1502 return false;
1503 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001504 StringStream buffer;
1505 bool result = this->toMetal(program, buffer);
1506 if (result) {
1507 *out = buffer.str();
1508 }
1509 return result;
1510}
1511
Ethan Nicholas00543112018-07-31 09:44:36 -04001512bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1513 if (!this->optimize(program)) {
1514 return false;
1515 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001516 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001517 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001518 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001519 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001520 return result;
1521}
1522
Ethan Nicholas00543112018-07-31 09:44:36 -04001523bool Compiler::toH(Program& program, String name, OutputStream& out) {
1524 if (!this->optimize(program)) {
1525 return false;
1526 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001527 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001528 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001529 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001530 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001531 return result;
1532}
1533
Brian Osman2e29ab52019-09-20 12:19:11 -04001534#endif
1535
1536#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Ethan Nicholas00543112018-07-31 09:44:36 -04001537bool Compiler::toPipelineStage(const Program& program, String* out,
Brian Osman2e29ab52019-09-20 12:19:11 -04001538 std::vector<FormatArg>* outFormatArgs,
1539 std::vector<GLSLFunction>* outFunctions) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001540 SkASSERT(program.fIsOptimized);
1541 fSource = program.fSource.get();
1542 StringStream buffer;
Brian Osman2e29ab52019-09-20 12:19:11 -04001543 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outFormatArgs,
1544 outFunctions);
Ethan Nicholas00543112018-07-31 09:44:36 -04001545 bool result = cg.generateCode();
1546 fSource = nullptr;
1547 if (result) {
1548 *out = buffer.str();
1549 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001550 return result;
1551}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001552#endif
1553
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001554std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001555#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001556 if (!this->optimize(program)) {
1557 return nullptr;
1558 }
1559 std::unique_ptr<ByteCode> result(new ByteCode());
1560 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1561 if (cg.generateCode()) {
1562 return result;
1563 }
Brian Osman489cf882019-07-09 10:48:28 -04001564#else
1565 ABORT("ByteCode interpreter not enabled");
1566#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001567 return nullptr;
1568}
1569
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001570const char* Compiler::OperatorName(Token::Kind kind) {
1571 switch (kind) {
1572 case Token::PLUS: return "+";
1573 case Token::MINUS: return "-";
1574 case Token::STAR: return "*";
1575 case Token::SLASH: return "/";
1576 case Token::PERCENT: return "%";
1577 case Token::SHL: return "<<";
1578 case Token::SHR: return ">>";
1579 case Token::LOGICALNOT: return "!";
1580 case Token::LOGICALAND: return "&&";
1581 case Token::LOGICALOR: return "||";
1582 case Token::LOGICALXOR: return "^^";
1583 case Token::BITWISENOT: return "~";
1584 case Token::BITWISEAND: return "&";
1585 case Token::BITWISEOR: return "|";
1586 case Token::BITWISEXOR: return "^";
1587 case Token::EQ: return "=";
1588 case Token::EQEQ: return "==";
1589 case Token::NEQ: return "!=";
1590 case Token::LT: return "<";
1591 case Token::GT: return ">";
1592 case Token::LTEQ: return "<=";
1593 case Token::GTEQ: return ">=";
1594 case Token::PLUSEQ: return "+=";
1595 case Token::MINUSEQ: return "-=";
1596 case Token::STAREQ: return "*=";
1597 case Token::SLASHEQ: return "/=";
1598 case Token::PERCENTEQ: return "%=";
1599 case Token::SHLEQ: return "<<=";
1600 case Token::SHREQ: return ">>=";
1601 case Token::LOGICALANDEQ: return "&&=";
1602 case Token::LOGICALOREQ: return "||=";
1603 case Token::LOGICALXOREQ: return "^^=";
1604 case Token::BITWISEANDEQ: return "&=";
1605 case Token::BITWISEOREQ: return "|=";
1606 case Token::BITWISEXOREQ: return "^=";
1607 case Token::PLUSPLUS: return "++";
1608 case Token::MINUSMINUS: return "--";
1609 case Token::COMMA: return ",";
1610 default:
1611 ABORT("unsupported operator: %d\n", kind);
1612 }
1613}
1614
1615
1616bool Compiler::IsAssignment(Token::Kind op) {
1617 switch (op) {
1618 case Token::EQ: // fall through
1619 case Token::PLUSEQ: // fall through
1620 case Token::MINUSEQ: // fall through
1621 case Token::STAREQ: // fall through
1622 case Token::SLASHEQ: // fall through
1623 case Token::PERCENTEQ: // fall through
1624 case Token::SHLEQ: // fall through
1625 case Token::SHREQ: // fall through
1626 case Token::BITWISEOREQ: // fall through
1627 case Token::BITWISEXOREQ: // fall through
1628 case Token::BITWISEANDEQ: // fall through
1629 case Token::LOGICALOREQ: // fall through
1630 case Token::LOGICALXOREQ: // fall through
1631 case Token::LOGICALANDEQ:
1632 return true;
1633 default:
1634 return false;
1635 }
1636}
1637
1638Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001639 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001640 int line = 1;
1641 int column = 1;
1642 for (int i = 0; i < offset; i++) {
1643 if ((*fSource)[i] == '\n') {
1644 ++line;
1645 column = 1;
1646 }
1647 else {
1648 ++column;
1649 }
1650 }
1651 return Position(line, column);
1652}
1653
1654void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001655 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001656 Position pos = this->position(offset);
1657 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001658}
1659
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001660String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001661 this->writeErrorCount();
1662 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001663 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001664 return result;
1665}
1666
1667void Compiler::writeErrorCount() {
1668 if (fErrorCount) {
1669 fErrorText += to_string(fErrorCount) + " error";
1670 if (fErrorCount > 1) {
1671 fErrorText += "s";
1672 }
1673 fErrorText += "\n";
1674 }
1675}
1676
ethannicholasb3058bd2016-07-01 08:22:01 -07001677} // namespace