blob: 92c0c03970378c11a55ea226c137626d205466ce [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"
Brian Osmanc0243912020-02-19 15:35:26 -050019#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/ir/SkSLEnum.h"
21#include "src/sksl/ir/SkSLExpression.h"
22#include "src/sksl/ir/SkSLExpressionStatement.h"
23#include "src/sksl/ir/SkSLFunctionCall.h"
24#include "src/sksl/ir/SkSLIntLiteral.h"
25#include "src/sksl/ir/SkSLModifiersDeclaration.h"
26#include "src/sksl/ir/SkSLNop.h"
27#include "src/sksl/ir/SkSLSymbolTable.h"
28#include "src/sksl/ir/SkSLTernaryExpression.h"
29#include "src/sksl/ir/SkSLUnresolvedFunction.h"
30#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070031
Ethan Nicholasa11035b2019-11-26 16:27:47 -050032#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
33#include "include/gpu/GrContextOptions.h"
34#include "src/gpu/GrShaderCaps.h"
35#endif
36
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040037#ifdef SK_ENABLE_SPIRV_VALIDATION
38#include "spirv-tools/libspirv.hpp"
39#endif
40
ethannicholasb3058bd2016-07-01 08:22:01 -070041// include the built-in shader symbols as static strings
42
Ethan Nicholas79707652017-11-16 11:20:11 -050043#define STRINGIFY(x) #x
44
Ethan Nicholas8da1e652019-05-24 11:01:59 -040045static const char* SKSL_GPU_INCLUDE =
46#include "sksl_gpu.inc"
47;
48
Brian Salomonf8c187c2019-12-19 14:41:57 -050049static const char* SKSL_BLEND_INCLUDE =
50#include "sksl_blend.inc"
51;
52
Ethan Nicholas8da1e652019-05-24 11:01:59 -040053static const char* SKSL_INTERP_INCLUDE =
54#include "sksl_interp.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070055;
56
ethannicholas5961bc92016-10-12 06:39:56 -070057static const char* SKSL_VERT_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050058#include "sksl_vert.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070059;
60
ethannicholas5961bc92016-10-12 06:39:56 -070061static const char* SKSL_FRAG_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050062#include "sksl_frag.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070063;
64
Ethan Nicholas52cad152017-02-16 16:37:32 -050065static const char* SKSL_GEOM_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050066#include "sksl_geom.inc"
Ethan Nicholas52cad152017-02-16 16:37:32 -050067;
68
Ethan Nicholas762466e2017-06-29 10:03:38 -040069static const char* SKSL_FP_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050070#include "sksl_enums.inc"
71#include "sksl_fp.inc"
Ethan Nicholas762466e2017-06-29 10:03:38 -040072;
73
Ethan Nicholas8da1e652019-05-24 11:01:59 -040074static const char* SKSL_PIPELINE_INCLUDE =
75#include "sksl_pipeline.inc"
Ethan Nicholas0d997662019-04-08 09:46:01 -040076;
77
ethannicholasb3058bd2016-07-01 08:22:01 -070078namespace SkSL {
79
Ethan Nicholasdb80f692019-11-22 14:06:12 -050080static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Ethan Nicholasb962eff2020-01-23 16:49:41 -050081 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
82 for (auto iter = src->begin(); iter != src->end(); ) {
83 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050084 switch (element->fKind) {
85 case ProgramElement::kFunction_Kind: {
86 FunctionDefinition& f = (FunctionDefinition&) *element;
Ethan Nicholasb962eff2020-01-23 16:49:41 -050087 SkASSERT(f.fDeclaration.fBuiltin);
88 String key = f.fDeclaration.declaration();
89 SkASSERT(target->find(key) == target->end());
90 (*target)[key] = std::make_pair(std::move(element), false);
91 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050092 break;
93 }
94 case ProgramElement::kEnum_Kind: {
95 Enum& e = (Enum&) *element;
96 StringFragment name = e.fTypeName;
97 SkASSERT(target->find(name) == target->end());
98 (*target)[name] = std::make_pair(std::move(element), false);
Ethan Nicholasb962eff2020-01-23 16:49:41 -050099 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500100 break;
101 }
102 default:
103 printf("unsupported include file element\n");
104 SkASSERT(false);
105 }
106 }
107}
108
109
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400110Compiler::Compiler(Flags flags)
111: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400112, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400113, fErrorCount(0) {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400114 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
115 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400116 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700117 fTypes = types;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400118 #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
119 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 ADD_TYPE(Void);
121 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400122 ADD_TYPE(Float2);
123 ADD_TYPE(Float3);
124 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400125 ADD_TYPE(Half);
126 ADD_TYPE(Half2);
127 ADD_TYPE(Half3);
128 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700129 ADD_TYPE(Double);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400130 ADD_TYPE(Double2);
131 ADD_TYPE(Double3);
132 ADD_TYPE(Double4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700133 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400134 ADD_TYPE(Int2);
135 ADD_TYPE(Int3);
136 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700137 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400138 ADD_TYPE(UInt2);
139 ADD_TYPE(UInt3);
140 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400141 ADD_TYPE(Short);
142 ADD_TYPE(Short2);
143 ADD_TYPE(Short3);
144 ADD_TYPE(Short4);
145 ADD_TYPE(UShort);
146 ADD_TYPE(UShort2);
147 ADD_TYPE(UShort3);
148 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400149 ADD_TYPE(Byte);
150 ADD_TYPE(Byte2);
151 ADD_TYPE(Byte3);
152 ADD_TYPE(Byte4);
153 ADD_TYPE(UByte);
154 ADD_TYPE(UByte2);
155 ADD_TYPE(UByte3);
156 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700157 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400158 ADD_TYPE(Bool2);
159 ADD_TYPE(Bool3);
160 ADD_TYPE(Bool4);
161 ADD_TYPE(Float2x2);
162 ADD_TYPE(Float2x3);
163 ADD_TYPE(Float2x4);
164 ADD_TYPE(Float3x2);
165 ADD_TYPE(Float3x3);
166 ADD_TYPE(Float3x4);
167 ADD_TYPE(Float4x2);
168 ADD_TYPE(Float4x3);
169 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400170 ADD_TYPE(Half2x2);
171 ADD_TYPE(Half2x3);
172 ADD_TYPE(Half2x4);
173 ADD_TYPE(Half3x2);
174 ADD_TYPE(Half3x3);
175 ADD_TYPE(Half3x4);
176 ADD_TYPE(Half4x2);
177 ADD_TYPE(Half4x3);
178 ADD_TYPE(Half4x4);
179 ADD_TYPE(Double2x2);
180 ADD_TYPE(Double2x3);
181 ADD_TYPE(Double2x4);
182 ADD_TYPE(Double3x2);
183 ADD_TYPE(Double3x3);
184 ADD_TYPE(Double3x4);
185 ADD_TYPE(Double4x2);
186 ADD_TYPE(Double4x3);
187 ADD_TYPE(Double4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400189 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190 ADD_TYPE(GenDType);
191 ADD_TYPE(GenIType);
192 ADD_TYPE(GenUType);
193 ADD_TYPE(GenBType);
194 ADD_TYPE(Mat);
195 ADD_TYPE(Vec);
196 ADD_TYPE(GVec);
197 ADD_TYPE(GVec2);
198 ADD_TYPE(GVec3);
199 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400200 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700201 ADD_TYPE(DVec);
202 ADD_TYPE(IVec);
203 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400204 ADD_TYPE(SVec);
205 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400206 ADD_TYPE(ByteVec);
207 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700208 ADD_TYPE(BVec);
209
210 ADD_TYPE(Sampler1D);
211 ADD_TYPE(Sampler2D);
212 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700213 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700214 ADD_TYPE(SamplerCube);
215 ADD_TYPE(Sampler2DRect);
216 ADD_TYPE(Sampler1DArray);
217 ADD_TYPE(Sampler2DArray);
218 ADD_TYPE(SamplerCubeArray);
219 ADD_TYPE(SamplerBuffer);
220 ADD_TYPE(Sampler2DMS);
221 ADD_TYPE(Sampler2DMSArray);
222
Brian Salomonbf7b6202016-11-11 16:08:03 -0500223 ADD_TYPE(ISampler2D);
224
Brian Salomon2a51de82016-11-16 12:06:01 -0500225 ADD_TYPE(Image2D);
226 ADD_TYPE(IImage2D);
227
Greg Daniel64773e62016-11-22 09:44:03 -0500228 ADD_TYPE(SubpassInput);
229 ADD_TYPE(SubpassInputMS);
230
ethannicholasb3058bd2016-07-01 08:22:01 -0700231 ADD_TYPE(GSampler1D);
232 ADD_TYPE(GSampler2D);
233 ADD_TYPE(GSampler3D);
234 ADD_TYPE(GSamplerCube);
235 ADD_TYPE(GSampler2DRect);
236 ADD_TYPE(GSampler1DArray);
237 ADD_TYPE(GSampler2DArray);
238 ADD_TYPE(GSamplerCubeArray);
239 ADD_TYPE(GSamplerBuffer);
240 ADD_TYPE(GSampler2DMS);
241 ADD_TYPE(GSampler2DMSArray);
242
243 ADD_TYPE(Sampler1DShadow);
244 ADD_TYPE(Sampler2DShadow);
245 ADD_TYPE(SamplerCubeShadow);
246 ADD_TYPE(Sampler2DRectShadow);
247 ADD_TYPE(Sampler1DArrayShadow);
248 ADD_TYPE(Sampler2DArrayShadow);
249 ADD_TYPE(SamplerCubeArrayShadow);
250 ADD_TYPE(GSampler2DArrayShadow);
251 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400252 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400253 ADD_TYPE(Sampler);
254 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700255
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700256 StringFragment skCapsName("sk_Caps");
257 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400258 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500259 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
260
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700261 StringFragment skArgsName("sk_Args");
262 Variable* skArgs = new Variable(-1, Modifiers(), skArgsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400263 *fContext->fSkArgs_Type, Variable::kGlobal_Storage);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400264 fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs));
265
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500266 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
267 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400268 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500269 symbols, &gpuIntrinsics, &fGpuSymbolTable);
Brian Salomonf8c187c2019-12-19 14:41:57 -0500270 this->processIncludeFile(Program::kFragment_Kind, SKSL_BLEND_INCLUDE,
271 strlen(SKSL_BLEND_INCLUDE), std::move(fGpuSymbolTable), &gpuIntrinsics,
272 &fGpuSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500273 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
274 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
275 // remain valid
276 fGpuIncludeSource = std::move(fIRGenerator->fFile);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400277 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
278 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
279 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
280 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
281 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, strlen(SKSL_GEOM_INCLUDE),
282 fGpuSymbolTable, &fGeometryInclude, &fGeometrySymbolTable);
283 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
284 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
285 &fPipelineSymbolTable);
286 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
287 strlen(SKSL_INTERP_INCLUDE), symbols, &fInterpreterInclude,
288 &fInterpreterSymbolTable);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500289 grab_intrinsics(&fInterpreterInclude, &fInterpreterIntrinsics);
290 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
291 // remain valid
292 fInterpreterIncludeSource = std::move(fIRGenerator->fFile);
ethannicholasb3058bd2016-07-01 08:22:01 -0700293}
294
295Compiler::~Compiler() {
296 delete fIRGenerator;
297}
298
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400299void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
300 std::shared_ptr<SymbolTable> base,
301 std::vector<std::unique_ptr<ProgramElement>>* outElements,
302 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500303#ifdef SK_DEBUG
304 String source(src, length);
305 fSource = &source;
306#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400307 fIRGenerator->fSymbolTable = std::move(base);
308 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500309#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
310 GrContextOptions opts;
311 GrShaderCaps caps(opts);
312 settings.fCaps = &caps;
313#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400314 fIRGenerator->start(&settings, nullptr);
315 fIRGenerator->convertProgram(kind, src, length, *fTypes, outElements);
316 if (this->fErrorCount) {
317 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
318 }
319 SkASSERT(!fErrorCount);
320 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
321 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500322#ifdef SK_DEBUG
323 fSource = nullptr;
324#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400325}
326
ethannicholas22f939e2016-10-13 13:25:34 -0700327// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500328void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
329 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700330 switch (lvalue->fKind) {
331 case Expression::kVariableReference_Kind: {
332 const Variable& var = ((VariableReference*) lvalue)->fVariable;
333 if (var.fStorage == Variable::kLocal_Storage) {
334 (*definitions)[&var] = expr;
335 }
336 break;
337 }
338 case Expression::kSwizzle_Kind:
339 // We consider the variable written to as long as at least some of its components have
340 // been written to. This will lead to some false negatives (we won't catch it if you
341 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400342 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
343 // 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 -0700344 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400345 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400346 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700347 definitions);
348 break;
349 case Expression::kIndex_Kind:
350 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400351 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400352 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700353 definitions);
354 break;
355 case Expression::kFieldAccess_Kind:
356 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400357 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400358 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700359 definitions);
360 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500361 case Expression::kTernary_Kind:
362 // To simplify analysis, we just pretend that we write to both sides of the ternary.
363 // This allows for false positives (meaning we fail to detect that a variable might not
364 // have been assigned), but is preferable to false negatives.
365 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400366 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500367 definitions);
368 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400369 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500370 definitions);
371 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400372 case Expression::kExternalValue_Kind:
373 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700374 default:
375 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400376 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700377 }
378}
379
380// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400381void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500382 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700383 switch (node.fKind) {
384 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400385 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400386 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500387 switch (expr->fKind) {
388 case Expression::kBinary_Kind: {
389 BinaryExpression* b = (BinaryExpression*) expr;
390 if (b->fOperator == Token::EQ) {
391 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700392 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500393 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400394 b->fLeft.get(),
395 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
396 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500397
398 }
399 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700400 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400401 case Expression::kFunctionCall_Kind: {
402 const FunctionCall& c = (const FunctionCall&) *expr;
403 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
404 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
405 this->addDefinition(
406 c.fArguments[i].get(),
407 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
408 definitions);
409 }
410 }
411 break;
412 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500413 case Expression::kPrefix_Kind: {
414 const PrefixExpression* p = (PrefixExpression*) expr;
415 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
416 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400417 p->fOperand.get(),
418 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
419 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500420 }
421 break;
422 }
423 case Expression::kPostfix_Kind: {
424 const PostfixExpression* p = (PostfixExpression*) expr;
425 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
426 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400427 p->fOperand.get(),
428 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
429 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500430 }
431 break;
432 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400433 case Expression::kVariableReference_Kind: {
434 const VariableReference* v = (VariableReference*) expr;
435 if (v->fRefKind != VariableReference::kRead_RefKind) {
436 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400437 v,
438 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
439 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400440 }
441 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500442 default:
443 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700444 }
445 break;
446 }
447 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400448 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000449 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
450 VarDeclaration& vd = (VarDeclaration&) *stmt;
451 if (vd.fValue) {
452 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700453 }
454 }
455 break;
456 }
457 }
458}
459
460void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
461 BasicBlock& block = cfg->fBlocks[blockId];
462
463 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500464 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700465 for (const BasicBlock::Node& n : block.fNodes) {
466 this->addDefinitions(n, &after);
467 }
468
469 // propagate definitions to exits
470 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400471 if (exitId == blockId) {
472 continue;
473 }
ethannicholas22f939e2016-10-13 13:25:34 -0700474 BasicBlock& exit = cfg->fBlocks[exitId];
475 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500476 std::unique_ptr<Expression>* e1 = pair.second;
477 auto found = exit.fBefore.find(pair.first);
478 if (found == exit.fBefore.end()) {
479 // exit has no definition for it, just copy it
480 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700481 exit.fBefore[pair.first] = e1;
482 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500483 // exit has a (possibly different) value already defined
484 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700485 if (e1 != e2) {
486 // definition has changed, merge and add exit block to worklist
487 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500488 if (e1 && e2) {
489 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400490 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500491 } else {
492 exit.fBefore[pair.first] = nullptr;
493 }
ethannicholas22f939e2016-10-13 13:25:34 -0700494 }
495 }
496 }
497 }
498}
499
500// returns a map which maps all local variables in the function to null, indicating that their value
501// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500502static DefinitionMap compute_start_state(const CFG& cfg) {
503 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400504 for (const auto& block : cfg.fBlocks) {
505 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700506 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400507 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400508 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700509 if (s->fKind == Statement::kVarDeclarations_Kind) {
510 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000511 for (const auto& decl : vd->fDeclaration->fVars) {
512 if (decl->fKind == Statement::kVarDeclaration_Kind) {
513 result[((VarDeclaration&) *decl).fVar] = nullptr;
514 }
Mike Klein6ad99092016-10-26 10:35:22 -0400515 }
ethannicholas22f939e2016-10-13 13:25:34 -0700516 }
517 }
518 }
519 }
520 return result;
521}
522
Ethan Nicholascb670962017-04-20 19:31:52 -0400523/**
524 * Returns true if assigning to this lvalue has no effect.
525 */
526static bool is_dead(const Expression& lvalue) {
527 switch (lvalue.fKind) {
528 case Expression::kVariableReference_Kind:
529 return ((VariableReference&) lvalue).fVariable.dead();
530 case Expression::kSwizzle_Kind:
531 return is_dead(*((Swizzle&) lvalue).fBase);
532 case Expression::kFieldAccess_Kind:
533 return is_dead(*((FieldAccess&) lvalue).fBase);
534 case Expression::kIndex_Kind: {
535 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500536 return is_dead(*idx.fBase) &&
537 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400538 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500539 case Expression::kTernary_Kind: {
540 const TernaryExpression& t = (TernaryExpression&) lvalue;
541 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
542 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400543 case Expression::kExternalValue_Kind:
544 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400545 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500546#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400547 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500548#endif
549 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400550 }
551}
ethannicholas22f939e2016-10-13 13:25:34 -0700552
Ethan Nicholascb670962017-04-20 19:31:52 -0400553/**
554 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
555 * to a dead target and lack of side effects on the left hand side.
556 */
557static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700558 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400559 return false;
560 }
561 return is_dead(*b.fLeft);
562}
563
564void Compiler::computeDataFlow(CFG* cfg) {
565 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700566 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400567 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700568 workList.insert(i);
569 }
570 while (workList.size()) {
571 BlockId next = *workList.begin();
572 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400573 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700574 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400575}
576
577/**
578 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
579 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
580 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
581 * need to be regenerated).
582 */
583bool try_replace_expression(BasicBlock* b,
584 std::vector<BasicBlock::Node>::iterator* iter,
585 std::unique_ptr<Expression>* newExpression) {
586 std::unique_ptr<Expression>* target = (*iter)->expression();
587 if (!b->tryRemoveExpression(iter)) {
588 *target = std::move(*newExpression);
589 return false;
590 }
591 *target = std::move(*newExpression);
592 return b->tryInsertExpression(iter, target);
593}
594
595/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400596 * Returns true if the expression is a constant numeric literal with the specified value, or a
597 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400598 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400599bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400600 switch (expr.fKind) {
601 case Expression::kIntLiteral_Kind:
602 return ((IntLiteral&) expr).fValue == value;
603 case Expression::kFloatLiteral_Kind:
604 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400605 case Expression::kConstructor_Kind: {
606 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400607 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
608 : c.fType.isFloat();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400609 if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) {
610 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400611 if (isFloat) {
612 if (c.getFVecComponent(i) != value) {
613 return false;
614 }
615 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400616 return false;
617 }
618 }
619 return true;
620 }
621 return false;
622 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400623 default:
624 return false;
625 }
626}
627
628/**
629 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
630 * and CFG structures).
631 */
632void delete_left(BasicBlock* b,
633 std::vector<BasicBlock::Node>::iterator* iter,
634 bool* outUpdated,
635 bool* outNeedsRescan) {
636 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400637 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400638 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400639 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400640 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400641 bool result;
642 if (bin.fOperator == Token::EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400643 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400644 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400645 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400646 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400647 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400648 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400649 *outNeedsRescan = true;
650 return;
651 }
652 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400653 *outNeedsRescan = true;
654 return;
655 }
656 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400657 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
658 (*iter)->expression() != &bin.fRight) {
659 *outNeedsRescan = true;
660 return;
661 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400662 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400663 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400664}
665
666/**
667 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
668 * CFG structures).
669 */
670void delete_right(BasicBlock* b,
671 std::vector<BasicBlock::Node>::iterator* iter,
672 bool* outUpdated,
673 bool* outNeedsRescan) {
674 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400675 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400676 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400677 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400678 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400679 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
680 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400681 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400682 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400683 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400684 *target = std::move(bin.fLeft);
685 if (*iter == b->fNodes.begin()) {
686 *outNeedsRescan = true;
687 return;
688 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400689 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400690 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
691 (*iter)->expression() != &bin.fLeft)) {
692 *outNeedsRescan = true;
693 return;
694 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400695 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400696 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400697}
698
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400699/**
700 * Constructs the specified type using a single argument.
701 */
702static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
703 std::vector<std::unique_ptr<Expression>> args;
704 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700705 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400706 return result;
707}
708
709/**
710 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
711 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
712 */
713static void vectorize(BasicBlock* b,
714 std::vector<BasicBlock::Node>::iterator* iter,
715 const Type& type,
716 std::unique_ptr<Expression>* otherExpression,
717 bool* outUpdated,
718 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400719 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
720 SkASSERT(type.kind() == Type::kVector_Kind);
721 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400722 *outUpdated = true;
723 std::unique_ptr<Expression>* target = (*iter)->expression();
724 if (!b->tryRemoveExpression(iter)) {
725 *target = construct(type, std::move(*otherExpression));
726 *outNeedsRescan = true;
727 } else {
728 *target = construct(type, std::move(*otherExpression));
729 if (!b->tryInsertExpression(iter, target)) {
730 *outNeedsRescan = true;
731 }
732 }
733}
734
735/**
736 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
737 * left to yield vec<n>(x).
738 */
739static void vectorize_left(BasicBlock* b,
740 std::vector<BasicBlock::Node>::iterator* iter,
741 bool* outUpdated,
742 bool* outNeedsRescan) {
743 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
744 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
745}
746
747/**
748 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
749 * right to yield vec<n>(y).
750 */
751static void vectorize_right(BasicBlock* b,
752 std::vector<BasicBlock::Node>::iterator* iter,
753 bool* outUpdated,
754 bool* outNeedsRescan) {
755 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
756 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
757}
758
759// Mark that an expression which we were writing to is no longer being written to
760void clear_write(const Expression& expr) {
761 switch (expr.fKind) {
762 case Expression::kVariableReference_Kind: {
763 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
764 break;
765 }
766 case Expression::kFieldAccess_Kind:
767 clear_write(*((FieldAccess&) expr).fBase);
768 break;
769 case Expression::kSwizzle_Kind:
770 clear_write(*((Swizzle&) expr).fBase);
771 break;
772 case Expression::kIndex_Kind:
773 clear_write(*((IndexExpression&) expr).fBase);
774 break;
775 default:
776 ABORT("shouldn't be writing to this kind of expression\n");
777 break;
778 }
779}
780
Ethan Nicholascb670962017-04-20 19:31:52 -0400781void Compiler::simplifyExpression(DefinitionMap& definitions,
782 BasicBlock& b,
783 std::vector<BasicBlock::Node>::iterator* iter,
784 std::unordered_set<const Variable*>* undefinedVariables,
785 bool* outUpdated,
786 bool* outNeedsRescan) {
787 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400788 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400789 if ((*iter)->fConstantPropagation) {
790 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
791 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400792 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400793 if (!try_replace_expression(&b, iter, &optimized)) {
794 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400795 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400796 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400797 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400798 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400799 }
800 }
801 switch (expr->fKind) {
802 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400803 const VariableReference& ref = (VariableReference&) *expr;
804 const Variable& var = ref.fVariable;
805 if (ref.refKind() != VariableReference::kWrite_RefKind &&
806 ref.refKind() != VariableReference::kPointer_RefKind &&
807 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400808 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
809 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000810 this->error(expr->fOffset,
811 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400812 }
813 break;
814 }
815 case Expression::kTernary_Kind: {
816 TernaryExpression* t = (TernaryExpression*) expr;
817 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
818 // ternary has a constant test, replace it with either the true or
819 // false branch
820 if (((BoolLiteral&) *t->fTest).fValue) {
821 (*iter)->setExpression(std::move(t->fIfTrue));
822 } else {
823 (*iter)->setExpression(std::move(t->fIfFalse));
824 }
825 *outUpdated = true;
826 *outNeedsRescan = true;
827 }
828 break;
829 }
830 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400831 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400832 if (dead_assignment(*bin)) {
833 delete_left(&b, iter, outUpdated, outNeedsRescan);
834 break;
835 }
836 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400837 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
838 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
839 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
840 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
841 break;
842 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400843 switch (bin->fOperator) {
844 case Token::STAR:
845 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400846 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
847 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400848 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400849 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
850 } else {
851 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400852 // 1 * float4(x) -> float4(x)
853 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400854 delete_left(&b, iter, outUpdated, outNeedsRescan);
855 }
856 }
857 else if (is_constant(*bin->fLeft, 0)) {
858 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500859 bin->fRight->fType.kind() == Type::kVector_Kind &&
860 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400861 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400862 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
863 } else {
864 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400865 // float4(0) * x -> float4(0)
866 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500867 if (!bin->fRight->hasSideEffects()) {
868 delete_right(&b, iter, outUpdated, outNeedsRescan);
869 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400870 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400871 }
872 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400873 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
874 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400875 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400876 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
877 } else {
878 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400879 // float4(x) * 1 -> float4(x)
880 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400881 delete_right(&b, iter, outUpdated, outNeedsRescan);
882 }
883 }
884 else if (is_constant(*bin->fRight, 0)) {
885 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500886 bin->fRight->fType.kind() == Type::kScalar_Kind &&
887 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400888 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400889 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
890 } else {
891 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400892 // x * float4(0) -> float4(0)
893 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500894 if (!bin->fLeft->hasSideEffects()) {
895 delete_left(&b, iter, outUpdated, outNeedsRescan);
896 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400897 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400898 }
899 break;
Ethan Nicholas56e42712017-04-21 10:23:37 -0400900 case Token::PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400901 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400902 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
903 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400904 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400905 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
906 } else {
907 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400908 // 0 + float4(x) -> float4(x)
909 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400910 delete_left(&b, iter, outUpdated, outNeedsRescan);
911 }
912 } else if (is_constant(*bin->fRight, 0)) {
913 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
914 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400915 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400916 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
917 } else {
918 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400919 // float4(x) + 0 -> float4(x)
920 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400921 delete_right(&b, iter, outUpdated, outNeedsRescan);
922 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400923 }
924 break;
925 case Token::MINUS:
926 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400927 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
928 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400929 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400930 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
931 } else {
932 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400933 // float4(x) - 0 -> float4(x)
934 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400935 delete_right(&b, iter, outUpdated, outNeedsRescan);
936 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400937 }
938 break;
939 case Token::SLASH:
940 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400941 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
942 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400943 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400944 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
945 } else {
946 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400947 // float4(x) / 1 -> float4(x)
948 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400949 delete_right(&b, iter, outUpdated, outNeedsRescan);
950 }
951 } else if (is_constant(*bin->fLeft, 0)) {
952 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500953 bin->fRight->fType.kind() == Type::kVector_Kind &&
954 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400955 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400956 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
957 } else {
958 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400959 // float4(0) / x -> float4(0)
960 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500961 if (!bin->fRight->hasSideEffects()) {
962 delete_right(&b, iter, outUpdated, outNeedsRescan);
963 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400964 }
965 }
966 break;
967 case Token::PLUSEQ:
968 if (is_constant(*bin->fRight, 0)) {
969 clear_write(*bin->fLeft);
970 delete_right(&b, iter, outUpdated, outNeedsRescan);
971 }
972 break;
973 case Token::MINUSEQ:
974 if (is_constant(*bin->fRight, 0)) {
975 clear_write(*bin->fLeft);
976 delete_right(&b, iter, outUpdated, outNeedsRescan);
977 }
978 break;
979 case Token::STAREQ:
980 if (is_constant(*bin->fRight, 1)) {
981 clear_write(*bin->fLeft);
982 delete_right(&b, iter, outUpdated, outNeedsRescan);
983 }
984 break;
985 case Token::SLASHEQ:
986 if (is_constant(*bin->fRight, 1)) {
987 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400988 delete_right(&b, iter, outUpdated, outNeedsRescan);
989 }
990 break;
991 default:
992 break;
993 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -0400994 break;
995 }
996 case Expression::kSwizzle_Kind: {
997 Swizzle& s = (Swizzle&) *expr;
998 // detect identity swizzles like foo.rgba
999 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1000 bool identity = true;
1001 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1002 if (s.fComponents[i] != i) {
1003 identity = false;
1004 break;
1005 }
1006 }
1007 if (identity) {
1008 *outUpdated = true;
1009 if (!try_replace_expression(&b, iter, &s.fBase)) {
1010 *outNeedsRescan = true;
1011 return;
1012 }
1013 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1014 break;
1015 }
1016 }
1017 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1018 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1019 Swizzle& base = (Swizzle&) *s.fBase;
1020 std::vector<int> final;
1021 for (int c : s.fComponents) {
1022 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1023 final.push_back(c);
1024 } else {
1025 final.push_back(base.fComponents[c]);
1026 }
1027 }
1028 *outUpdated = true;
1029 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1030 std::move(final)));
1031 if (!try_replace_expression(&b, iter, &replacement)) {
1032 *outNeedsRescan = true;
1033 return;
1034 }
1035 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1036 break;
1037 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001038 }
1039 default:
1040 break;
1041 }
1042}
1043
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001044// returns true if this statement could potentially execute a break at the current level (we ignore
1045// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
Ethan Nicholas5005a222018-08-24 13:06:27 -04001046static bool contains_conditional_break(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001047 switch (s.fKind) {
1048 case Statement::kBlock_Kind:
1049 for (const auto& sub : ((Block&) s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001050 if (contains_conditional_break(*sub, inConditional)) {
1051 return true;
1052 }
1053 }
1054 return false;
1055 case Statement::kBreak_Kind:
1056 return inConditional;
1057 case Statement::kIf_Kind: {
1058 const IfStatement& i = (IfStatement&) s;
1059 return contains_conditional_break(*i.fIfTrue, true) ||
1060 (i.fIfFalse && contains_conditional_break(*i.fIfFalse, true));
1061 }
1062 default:
1063 return false;
1064 }
1065}
1066
1067// returns true if this statement definitely executes a break at the current level (we ignore
1068// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1069static bool contains_unconditional_break(Statement& s) {
1070 switch (s.fKind) {
1071 case Statement::kBlock_Kind:
1072 for (const auto& sub : ((Block&) s).fStatements) {
1073 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001074 return true;
1075 }
1076 }
1077 return false;
1078 case Statement::kBreak_Kind:
1079 return true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001080 default:
1081 return false;
1082 }
1083}
1084
1085// Returns a block containing all of the statements that will be run if the given case matches
1086// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1087// broken by this call and must then be discarded).
1088// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1089// when break statements appear inside conditionals.
1090static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) {
1091 bool capturing = false;
1092 std::vector<std::unique_ptr<Statement>*> statementPtrs;
1093 for (const auto& current : s->fCases) {
1094 if (current.get() == c) {
1095 capturing = true;
1096 }
1097 if (capturing) {
1098 for (auto& stmt : current->fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001099 if (contains_conditional_break(*stmt, s->fKind == Statement::kIf_Kind)) {
1100 return nullptr;
1101 }
1102 if (contains_unconditional_break(*stmt)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001103 capturing = false;
1104 break;
1105 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001106 statementPtrs.push_back(&stmt);
1107 }
1108 if (!capturing) {
1109 break;
1110 }
1111 }
1112 }
1113 std::vector<std::unique_ptr<Statement>> statements;
1114 for (const auto& s : statementPtrs) {
1115 statements.push_back(std::move(*s));
1116 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001117 return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001118}
1119
Ethan Nicholascb670962017-04-20 19:31:52 -04001120void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001121 BasicBlock& b,
1122 std::vector<BasicBlock::Node>::iterator* iter,
1123 std::unordered_set<const Variable*>* undefinedVariables,
1124 bool* outUpdated,
1125 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001126 Statement* stmt = (*iter)->statement()->get();
1127 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001128 case Statement::kVarDeclaration_Kind: {
1129 const auto& varDecl = (VarDeclaration&) *stmt;
1130 if (varDecl.fVar->dead() &&
1131 (!varDecl.fValue ||
1132 !varDecl.fValue->hasSideEffects())) {
1133 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001134 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001135 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1136 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001137 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001138 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001139 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001140 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001141 }
1142 break;
1143 }
1144 case Statement::kIf_Kind: {
1145 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001146 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1147 // constant if, collapse down to a single branch
1148 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001149 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001150 (*iter)->setStatement(std::move(i.fIfTrue));
1151 } else {
1152 if (i.fIfFalse) {
1153 (*iter)->setStatement(std::move(i.fIfFalse));
1154 } else {
1155 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1156 }
1157 }
1158 *outUpdated = true;
1159 *outNeedsRescan = true;
1160 break;
1161 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001162 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1163 // else block doesn't do anything, remove it
1164 i.fIfFalse.reset();
1165 *outUpdated = true;
1166 *outNeedsRescan = true;
1167 }
1168 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1169 // if block doesn't do anything, no else block
1170 if (i.fTest->hasSideEffects()) {
1171 // test has side effects, keep it
1172 (*iter)->setStatement(std::unique_ptr<Statement>(
1173 new ExpressionStatement(std::move(i.fTest))));
1174 } else {
1175 // no if, no else, no test side effects, kill the whole if
1176 // statement
1177 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1178 }
1179 *outUpdated = true;
1180 *outNeedsRescan = true;
1181 }
1182 break;
1183 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001184 case Statement::kSwitch_Kind: {
1185 SwitchStatement& s = (SwitchStatement&) *stmt;
1186 if (s.fValue->isConstant()) {
1187 // switch is constant, replace it with the case that matches
1188 bool found = false;
1189 SwitchCase* defaultCase = nullptr;
1190 for (const auto& c : s.fCases) {
1191 if (!c->fValue) {
1192 defaultCase = c.get();
1193 continue;
1194 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001195 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001196 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001197 if (found) {
1198 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1199 if (newBlock) {
1200 (*iter)->setStatement(std::move(newBlock));
1201 break;
1202 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001203 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001204 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001205 "static switch contains non-static conditional break");
1206 s.fIsStatic = false;
1207 }
1208 return; // can't simplify
1209 }
1210 }
1211 }
1212 if (!found) {
1213 // no matching case. use default if it exists, or kill the whole thing
1214 if (defaultCase) {
1215 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1216 if (newBlock) {
1217 (*iter)->setStatement(std::move(newBlock));
1218 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001219 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001220 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001221 "static switch contains non-static conditional break");
1222 s.fIsStatic = false;
1223 }
1224 return; // can't simplify
1225 }
1226 } else {
1227 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1228 }
1229 }
1230 *outUpdated = true;
1231 *outNeedsRescan = true;
1232 }
1233 break;
1234 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001235 case Statement::kExpression_Kind: {
1236 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001237 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001238 if (!e.fExpression->hasSideEffects()) {
1239 // Expression statement with no side effects, kill it
1240 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1241 *outNeedsRescan = true;
1242 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001243 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001244 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1245 *outUpdated = true;
1246 }
1247 break;
1248 }
1249 default:
1250 break;
1251 }
1252}
1253
1254void Compiler::scanCFG(FunctionDefinition& f) {
1255 CFG cfg = CFGGenerator().getCFG(f);
1256 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001257
1258 // check for unreachable code
1259 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001260 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001261 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001262 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001263 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1264 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001265 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001266 break;
1267 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001268 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001269 break;
1270 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001271 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001272 }
1273 }
1274 if (fErrorCount) {
1275 return;
1276 }
1277
Ethan Nicholascb670962017-04-20 19:31:52 -04001278 // check for dead code & undefined variables, perform constant propagation
1279 std::unordered_set<const Variable*> undefinedVariables;
1280 bool updated;
1281 bool needsRescan = false;
1282 do {
1283 if (needsRescan) {
1284 cfg = CFGGenerator().getCFG(f);
1285 this->computeDataFlow(&cfg);
1286 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001287 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001288
1289 updated = false;
1290 for (BasicBlock& b : cfg.fBlocks) {
1291 DefinitionMap definitions = b.fBefore;
1292
1293 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1294 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1295 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1296 &needsRescan);
1297 } else {
1298 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1299 &needsRescan);
1300 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001301 if (needsRescan) {
1302 break;
1303 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001304 this->addDefinitions(*iter, &definitions);
1305 }
1306 }
1307 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001308 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001309
Ethan Nicholas91a10532017-06-22 11:24:38 -04001310 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001311 for (BasicBlock& b : cfg.fBlocks) {
1312 DefinitionMap definitions = b.fBefore;
1313
Ethan Nicholas91a10532017-06-22 11:24:38 -04001314 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1316 const Statement& s = **iter->statement();
1317 switch (s.fKind) {
1318 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001319 if (((const IfStatement&) s).fIsStatic &&
1320 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001321 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001322 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001323 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001324 break;
1325 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001326 if (((const SwitchStatement&) s).fIsStatic &&
1327 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001328 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001329 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001330 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001331 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001332 case Statement::kVarDeclarations_Kind: {
1333 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1334 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1335 if ((*varIter)->fKind == Statement::kNop_Kind) {
1336 varIter = decls.fVars.erase(varIter);
1337 } else {
1338 ++varIter;
1339 }
1340 }
1341 if (!decls.fVars.size()) {
1342 iter = b.fNodes.erase(iter);
1343 } else {
1344 ++iter;
1345 }
1346 break;
1347 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001348 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001349 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001350 break;
1351 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001352 } else {
1353 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001354 }
1355 }
1356 }
1357
ethannicholas22f939e2016-10-13 13:25:34 -07001358 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001359 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001360 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001361 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1362 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001363 }
1364 }
1365}
1366
Ethan Nicholas91164d12019-05-15 15:29:54 -04001367void Compiler::registerExternalValue(ExternalValue* value) {
1368 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1369}
1370
1371Symbol* Compiler::takeOwnership(std::unique_ptr<Symbol> symbol) {
1372 return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol));
1373}
1374
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001375std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001376 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001377 fErrorText = "";
1378 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001379 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001380 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001381 switch (kind) {
1382 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001383 inherited = &fVertexInclude;
1384 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001385 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001386 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001387 break;
1388 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001389 inherited = &fFragmentInclude;
1390 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001391 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001392 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001393 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001394 case Program::kGeometry_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001395 inherited = &fGeometryInclude;
1396 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001397 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001398 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001399 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001400 case Program::kFragmentProcessor_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001401 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001402 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001403 fIRGenerator->start(&settings, nullptr);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001404 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001405 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes,
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001406 &elements);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001407 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001408 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001409 case Program::kPipelineStage_Kind:
1410 inherited = &fPipelineInclude;
1411 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001412 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001413 fIRGenerator->start(&settings, inherited);
1414 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001415 case Program::kGeneric_Kind:
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001416 inherited = &fInterpreterInclude;
1417 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001418 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001419 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001420 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001421 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001422 for (auto& element : elements) {
1423 if (element->fKind == ProgramElement::kEnum_Kind) {
1424 ((Enum&) *element).fBuiltin = true;
1425 }
1426 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001427 std::unique_ptr<String> textPtr(new String(std::move(text)));
1428 fSource = textPtr.get();
Robert Phillipsfe8da172018-01-24 14:52:02 +00001429 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001430 auto result = std::unique_ptr<Program>(new Program(kind,
1431 std::move(textPtr),
1432 settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001433 fContext,
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001434 inherited,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001435 std::move(elements),
1436 fIRGenerator->fSymbolTable,
1437 fIRGenerator->fInputs));
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001438 if (fErrorCount) {
1439 return nullptr;
1440 }
1441 return result;
1442}
1443
Ethan Nicholas00543112018-07-31 09:44:36 -04001444bool Compiler::optimize(Program& program) {
1445 SkASSERT(!fErrorCount);
1446 if (!program.fIsOptimized) {
1447 program.fIsOptimized = true;
1448 fIRGenerator->fKind = program.fKind;
1449 fIRGenerator->fSettings = &program.fSettings;
1450 for (auto& element : program) {
1451 if (element.fKind == ProgramElement::kFunction_Kind) {
1452 this->scanCFG((FunctionDefinition&) element);
1453 }
1454 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001455 if (program.fKind != Program::kFragmentProcessor_Kind) {
1456 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1457 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1458 VarDeclarations& vars = (VarDeclarations&) **iter;
1459 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1460 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1461 if (var.dead()) {
1462 varIter = vars.fVars.erase(varIter);
1463 } else {
1464 ++varIter;
1465 }
1466 }
1467 if (vars.fVars.size() == 0) {
1468 iter = program.fElements.erase(iter);
1469 continue;
1470 }
1471 }
1472 ++iter;
1473 }
1474 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001475 }
1476 return fErrorCount == 0;
1477}
1478
1479std::unique_ptr<Program> Compiler::specialize(
1480 Program& program,
1481 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1482 std::vector<std::unique_ptr<ProgramElement>> elements;
1483 for (const auto& e : program) {
1484 elements.push_back(e.clone());
1485 }
1486 Program::Settings settings;
1487 settings.fCaps = program.fSettings.fCaps;
1488 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1489 settings.fArgs.insert(*iter);
1490 }
Brian Osman808f0212020-01-21 15:36:47 -05001491 std::unique_ptr<String> sourceCopy(new String(*program.fSource));
Ethan Nicholas00543112018-07-31 09:44:36 -04001492 std::unique_ptr<Program> result(new Program(program.fKind,
Brian Osman808f0212020-01-21 15:36:47 -05001493 std::move(sourceCopy),
Ethan Nicholas00543112018-07-31 09:44:36 -04001494 settings,
1495 program.fContext,
1496 program.fInheritedElements,
1497 std::move(elements),
1498 program.fSymbols,
1499 program.fInputs));
1500 return result;
1501}
1502
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001503#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1504
Ethan Nicholas00543112018-07-31 09:44:36 -04001505bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1506 if (!this->optimize(program)) {
1507 return false;
1508 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001509#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001510 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001511 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001512 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001513 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001514 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001515 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001516 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001517 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001518 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001519 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1520 SkDebugf("SPIR-V validation error: %s\n", m);
1521 };
1522 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001523 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001524 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001525 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001526 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001527 }
1528#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001529 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001530 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001531 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001532 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001533#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001534 return result;
1535}
1536
Ethan Nicholas00543112018-07-31 09:44:36 -04001537bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001538 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001539 bool result = this->toSPIRV(program, buffer);
1540 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001541 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001542 }
1543 return result;
1544}
1545
Ethan Nicholas00543112018-07-31 09:44:36 -04001546bool Compiler::toGLSL(Program& program, OutputStream& out) {
1547 if (!this->optimize(program)) {
1548 return false;
1549 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001550 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001551 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001552 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001553 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001554 return result;
1555}
1556
Ethan Nicholas00543112018-07-31 09:44:36 -04001557bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001558 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001559 bool result = this->toGLSL(program, buffer);
1560 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001561 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001562 }
1563 return result;
1564}
1565
Brian Osmanc0243912020-02-19 15:35:26 -05001566bool Compiler::toHLSL(Program& program, String* out) {
1567 String spirv;
1568 if (!this->toSPIRV(program, &spirv)) {
1569 return false;
1570 }
1571
1572 return SPIRVtoHLSL(spirv, out);
1573}
1574
Ethan Nicholas00543112018-07-31 09:44:36 -04001575bool Compiler::toMetal(Program& program, OutputStream& out) {
1576 if (!this->optimize(program)) {
1577 return false;
1578 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001579 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001580 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001581 return result;
1582}
1583
Ethan Nicholas00543112018-07-31 09:44:36 -04001584bool Compiler::toMetal(Program& program, String* out) {
1585 if (!this->optimize(program)) {
1586 return false;
1587 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001588 StringStream buffer;
1589 bool result = this->toMetal(program, buffer);
1590 if (result) {
1591 *out = buffer.str();
1592 }
1593 return result;
1594}
1595
Ethan Nicholas00543112018-07-31 09:44:36 -04001596bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1597 if (!this->optimize(program)) {
1598 return false;
1599 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001600 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001601 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001602 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001603 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001604 return result;
1605}
1606
Ethan Nicholas00543112018-07-31 09:44:36 -04001607bool Compiler::toH(Program& program, String name, OutputStream& out) {
1608 if (!this->optimize(program)) {
1609 return false;
1610 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001611 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001612 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001613 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001614 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001615 return result;
1616}
1617
Brian Osman2e29ab52019-09-20 12:19:11 -04001618#endif
1619
1620#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osman107c6662019-12-30 15:02:30 -05001621bool Compiler::toPipelineStage(const Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001622 SkASSERT(program.fIsOptimized);
1623 fSource = program.fSource.get();
1624 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001625 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001626 bool result = cg.generateCode();
1627 fSource = nullptr;
1628 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001629 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001630 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001631 return result;
1632}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001633#endif
1634
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001635std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001636#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001637 if (!this->optimize(program)) {
1638 return nullptr;
1639 }
Brian Osman808f0212020-01-21 15:36:47 -05001640 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001641 std::unique_ptr<ByteCode> result(new ByteCode());
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001642 ByteCodeGenerator cg(&program, this, result.get());
1643 if (cg.generateCode()) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001644 return result;
1645 }
Brian Osman489cf882019-07-09 10:48:28 -04001646#else
1647 ABORT("ByteCode interpreter not enabled");
1648#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001649 return nullptr;
1650}
1651
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001652const char* Compiler::OperatorName(Token::Kind kind) {
1653 switch (kind) {
1654 case Token::PLUS: return "+";
1655 case Token::MINUS: return "-";
1656 case Token::STAR: return "*";
1657 case Token::SLASH: return "/";
1658 case Token::PERCENT: return "%";
1659 case Token::SHL: return "<<";
1660 case Token::SHR: return ">>";
1661 case Token::LOGICALNOT: return "!";
1662 case Token::LOGICALAND: return "&&";
1663 case Token::LOGICALOR: return "||";
1664 case Token::LOGICALXOR: return "^^";
1665 case Token::BITWISENOT: return "~";
1666 case Token::BITWISEAND: return "&";
1667 case Token::BITWISEOR: return "|";
1668 case Token::BITWISEXOR: return "^";
1669 case Token::EQ: return "=";
1670 case Token::EQEQ: return "==";
1671 case Token::NEQ: return "!=";
1672 case Token::LT: return "<";
1673 case Token::GT: return ">";
1674 case Token::LTEQ: return "<=";
1675 case Token::GTEQ: return ">=";
1676 case Token::PLUSEQ: return "+=";
1677 case Token::MINUSEQ: return "-=";
1678 case Token::STAREQ: return "*=";
1679 case Token::SLASHEQ: return "/=";
1680 case Token::PERCENTEQ: return "%=";
1681 case Token::SHLEQ: return "<<=";
1682 case Token::SHREQ: return ">>=";
1683 case Token::LOGICALANDEQ: return "&&=";
1684 case Token::LOGICALOREQ: return "||=";
1685 case Token::LOGICALXOREQ: return "^^=";
1686 case Token::BITWISEANDEQ: return "&=";
1687 case Token::BITWISEOREQ: return "|=";
1688 case Token::BITWISEXOREQ: return "^=";
1689 case Token::PLUSPLUS: return "++";
1690 case Token::MINUSMINUS: return "--";
1691 case Token::COMMA: return ",";
1692 default:
1693 ABORT("unsupported operator: %d\n", kind);
1694 }
1695}
1696
1697
1698bool Compiler::IsAssignment(Token::Kind op) {
1699 switch (op) {
1700 case Token::EQ: // fall through
1701 case Token::PLUSEQ: // fall through
1702 case Token::MINUSEQ: // fall through
1703 case Token::STAREQ: // fall through
1704 case Token::SLASHEQ: // fall through
1705 case Token::PERCENTEQ: // fall through
1706 case Token::SHLEQ: // fall through
1707 case Token::SHREQ: // fall through
1708 case Token::BITWISEOREQ: // fall through
1709 case Token::BITWISEXOREQ: // fall through
1710 case Token::BITWISEANDEQ: // fall through
1711 case Token::LOGICALOREQ: // fall through
1712 case Token::LOGICALXOREQ: // fall through
1713 case Token::LOGICALANDEQ:
1714 return true;
1715 default:
1716 return false;
1717 }
1718}
1719
1720Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001721 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001722 int line = 1;
1723 int column = 1;
1724 for (int i = 0; i < offset; i++) {
1725 if ((*fSource)[i] == '\n') {
1726 ++line;
1727 column = 1;
1728 }
1729 else {
1730 ++column;
1731 }
1732 }
1733 return Position(line, column);
1734}
1735
1736void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001737 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001738 Position pos = this->position(offset);
1739 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001740}
1741
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001742String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001743 this->writeErrorCount();
1744 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001745 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001746 return result;
1747}
1748
1749void Compiler::writeErrorCount() {
1750 if (fErrorCount) {
1751 fErrorText += to_string(fErrorCount) + " error";
1752 if (fErrorCount > 1) {
1753 fErrorText += "s";
1754 }
1755 fErrorText += "\n";
1756 }
1757}
1758
ethannicholasb3058bd2016-07-01 08:22:01 -07001759} // namespace