blob: 9ac276a9e13fe5f96ceaca3b7a45d163c3d77f1b [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,
Brian Osmanb08cc022020-04-02 11:38:40 -040081 std::map<StringFragment, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
82 for (auto& element : *src) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -050083 switch (element->fKind) {
84 case ProgramElement::kFunction_Kind: {
85 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osmanb08cc022020-04-02 11:38:40 -040086 StringFragment name = f.fDeclaration.fName;
87 SkASSERT(target->find(name) == target->end());
88 (*target)[name] = std::make_pair(std::move(element), false);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050089 break;
90 }
91 case ProgramElement::kEnum_Kind: {
92 Enum& e = (Enum&) *element;
93 StringFragment name = e.fTypeName;
94 SkASSERT(target->find(name) == target->end());
95 (*target)[name] = std::make_pair(std::move(element), false);
96 break;
97 }
98 default:
99 printf("unsupported include file element\n");
100 SkASSERT(false);
101 }
102 }
103}
104
105
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400106Compiler::Compiler(Flags flags)
107: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400108, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400109, fErrorCount(0) {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400110 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
111 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400112 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700113 fTypes = types;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400114 #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
115 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 ADD_TYPE(Void);
117 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400118 ADD_TYPE(Float2);
119 ADD_TYPE(Float3);
120 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400121 ADD_TYPE(Half);
122 ADD_TYPE(Half2);
123 ADD_TYPE(Half3);
124 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700125 ADD_TYPE(Double);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400126 ADD_TYPE(Double2);
127 ADD_TYPE(Double3);
128 ADD_TYPE(Double4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700129 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400130 ADD_TYPE(Int2);
131 ADD_TYPE(Int3);
132 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700133 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400134 ADD_TYPE(UInt2);
135 ADD_TYPE(UInt3);
136 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400137 ADD_TYPE(Short);
138 ADD_TYPE(Short2);
139 ADD_TYPE(Short3);
140 ADD_TYPE(Short4);
141 ADD_TYPE(UShort);
142 ADD_TYPE(UShort2);
143 ADD_TYPE(UShort3);
144 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400145 ADD_TYPE(Byte);
146 ADD_TYPE(Byte2);
147 ADD_TYPE(Byte3);
148 ADD_TYPE(Byte4);
149 ADD_TYPE(UByte);
150 ADD_TYPE(UByte2);
151 ADD_TYPE(UByte3);
152 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700153 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400154 ADD_TYPE(Bool2);
155 ADD_TYPE(Bool3);
156 ADD_TYPE(Bool4);
157 ADD_TYPE(Float2x2);
158 ADD_TYPE(Float2x3);
159 ADD_TYPE(Float2x4);
160 ADD_TYPE(Float3x2);
161 ADD_TYPE(Float3x3);
162 ADD_TYPE(Float3x4);
163 ADD_TYPE(Float4x2);
164 ADD_TYPE(Float4x3);
165 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400166 ADD_TYPE(Half2x2);
167 ADD_TYPE(Half2x3);
168 ADD_TYPE(Half2x4);
169 ADD_TYPE(Half3x2);
170 ADD_TYPE(Half3x3);
171 ADD_TYPE(Half3x4);
172 ADD_TYPE(Half4x2);
173 ADD_TYPE(Half4x3);
174 ADD_TYPE(Half4x4);
175 ADD_TYPE(Double2x2);
176 ADD_TYPE(Double2x3);
177 ADD_TYPE(Double2x4);
178 ADD_TYPE(Double3x2);
179 ADD_TYPE(Double3x3);
180 ADD_TYPE(Double3x4);
181 ADD_TYPE(Double4x2);
182 ADD_TYPE(Double4x3);
183 ADD_TYPE(Double4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400185 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186 ADD_TYPE(GenDType);
187 ADD_TYPE(GenIType);
188 ADD_TYPE(GenUType);
189 ADD_TYPE(GenBType);
190 ADD_TYPE(Mat);
191 ADD_TYPE(Vec);
192 ADD_TYPE(GVec);
193 ADD_TYPE(GVec2);
194 ADD_TYPE(GVec3);
195 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400196 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700197 ADD_TYPE(DVec);
198 ADD_TYPE(IVec);
199 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400200 ADD_TYPE(SVec);
201 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400202 ADD_TYPE(ByteVec);
203 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 ADD_TYPE(BVec);
205
206 ADD_TYPE(Sampler1D);
207 ADD_TYPE(Sampler2D);
208 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700209 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700210 ADD_TYPE(SamplerCube);
211 ADD_TYPE(Sampler2DRect);
212 ADD_TYPE(Sampler1DArray);
213 ADD_TYPE(Sampler2DArray);
214 ADD_TYPE(SamplerCubeArray);
215 ADD_TYPE(SamplerBuffer);
216 ADD_TYPE(Sampler2DMS);
217 ADD_TYPE(Sampler2DMSArray);
218
Brian Salomonbf7b6202016-11-11 16:08:03 -0500219 ADD_TYPE(ISampler2D);
220
Brian Salomon2a51de82016-11-16 12:06:01 -0500221 ADD_TYPE(Image2D);
222 ADD_TYPE(IImage2D);
223
Greg Daniel64773e62016-11-22 09:44:03 -0500224 ADD_TYPE(SubpassInput);
225 ADD_TYPE(SubpassInputMS);
226
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 ADD_TYPE(GSampler1D);
228 ADD_TYPE(GSampler2D);
229 ADD_TYPE(GSampler3D);
230 ADD_TYPE(GSamplerCube);
231 ADD_TYPE(GSampler2DRect);
232 ADD_TYPE(GSampler1DArray);
233 ADD_TYPE(GSampler2DArray);
234 ADD_TYPE(GSamplerCubeArray);
235 ADD_TYPE(GSamplerBuffer);
236 ADD_TYPE(GSampler2DMS);
237 ADD_TYPE(GSampler2DMSArray);
238
239 ADD_TYPE(Sampler1DShadow);
240 ADD_TYPE(Sampler2DShadow);
241 ADD_TYPE(SamplerCubeShadow);
242 ADD_TYPE(Sampler2DRectShadow);
243 ADD_TYPE(Sampler1DArrayShadow);
244 ADD_TYPE(Sampler2DArrayShadow);
245 ADD_TYPE(SamplerCubeArrayShadow);
246 ADD_TYPE(GSampler2DArrayShadow);
247 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400248 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400249 ADD_TYPE(Sampler);
250 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700252 StringFragment skCapsName("sk_Caps");
253 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400254 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500255 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
256
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700257 StringFragment skArgsName("sk_Args");
258 Variable* skArgs = new Variable(-1, Modifiers(), skArgsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400259 *fContext->fSkArgs_Type, Variable::kGlobal_Storage);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400260 fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs));
261
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500262 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
263 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400264 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500265 symbols, &gpuIntrinsics, &fGpuSymbolTable);
Brian Salomonf8c187c2019-12-19 14:41:57 -0500266 this->processIncludeFile(Program::kFragment_Kind, SKSL_BLEND_INCLUDE,
267 strlen(SKSL_BLEND_INCLUDE), std::move(fGpuSymbolTable), &gpuIntrinsics,
268 &fGpuSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500269 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
270 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
271 // remain valid
272 fGpuIncludeSource = std::move(fIRGenerator->fFile);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400273 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
274 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
275 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
276 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
277 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, strlen(SKSL_GEOM_INCLUDE),
278 fGpuSymbolTable, &fGeometryInclude, &fGeometrySymbolTable);
279 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
280 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
281 &fPipelineSymbolTable);
Brian Osmanb08cc022020-04-02 11:38:40 -0400282 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400283 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
284 strlen(SKSL_INTERP_INCLUDE), symbols, &fInterpreterInclude,
285 &fInterpreterSymbolTable);
Brian Osmanb08cc022020-04-02 11:38:40 -0400286 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700287}
288
289Compiler::~Compiler() {
290 delete fIRGenerator;
291}
292
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400293void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
294 std::shared_ptr<SymbolTable> base,
295 std::vector<std::unique_ptr<ProgramElement>>* outElements,
296 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500297#ifdef SK_DEBUG
298 String source(src, length);
299 fSource = &source;
300#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400301 fIRGenerator->fSymbolTable = std::move(base);
302 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500303#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
304 GrContextOptions opts;
305 GrShaderCaps caps(opts);
306 settings.fCaps = &caps;
307#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400308 fIRGenerator->start(&settings, nullptr);
309 fIRGenerator->convertProgram(kind, src, length, *fTypes, outElements);
310 if (this->fErrorCount) {
311 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
312 }
313 SkASSERT(!fErrorCount);
314 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
315 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500316#ifdef SK_DEBUG
317 fSource = nullptr;
318#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400319}
320
ethannicholas22f939e2016-10-13 13:25:34 -0700321// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500322void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
323 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700324 switch (lvalue->fKind) {
325 case Expression::kVariableReference_Kind: {
326 const Variable& var = ((VariableReference*) lvalue)->fVariable;
327 if (var.fStorage == Variable::kLocal_Storage) {
328 (*definitions)[&var] = expr;
329 }
330 break;
331 }
332 case Expression::kSwizzle_Kind:
333 // We consider the variable written to as long as at least some of its components have
334 // been written to. This will lead to some false negatives (we won't catch it if you
335 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400336 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
337 // 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 -0700338 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400339 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400340 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700341 definitions);
342 break;
343 case Expression::kIndex_Kind:
344 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400345 this->addDefinition(((IndexExpression*) 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::kFieldAccess_Kind:
350 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400351 this->addDefinition(((FieldAccess*) 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;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500355 case Expression::kTernary_Kind:
356 // To simplify analysis, we just pretend that we write to both sides of the ternary.
357 // This allows for false positives (meaning we fail to detect that a variable might not
358 // have been assigned), but is preferable to false negatives.
359 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400360 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500361 definitions);
362 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400363 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500364 definitions);
365 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400366 case Expression::kExternalValue_Kind:
367 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700368 default:
369 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400370 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700371 }
372}
373
374// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400375void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500376 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700377 switch (node.fKind) {
378 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400379 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400380 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500381 switch (expr->fKind) {
382 case Expression::kBinary_Kind: {
383 BinaryExpression* b = (BinaryExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400384 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500385 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700386 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500387 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400388 b->fLeft.get(),
389 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
390 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500391
392 }
393 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700394 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400395 case Expression::kFunctionCall_Kind: {
396 const FunctionCall& c = (const FunctionCall&) *expr;
397 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
398 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
399 this->addDefinition(
400 c.fArguments[i].get(),
401 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
402 definitions);
403 }
404 }
405 break;
406 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500407 case Expression::kPrefix_Kind: {
408 const PrefixExpression* p = (PrefixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400409 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
410 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500411 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400412 p->fOperand.get(),
413 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
414 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500415 }
416 break;
417 }
418 case Expression::kPostfix_Kind: {
419 const PostfixExpression* p = (PostfixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400420 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
421 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500422 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400423 p->fOperand.get(),
424 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
425 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500426 }
427 break;
428 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400429 case Expression::kVariableReference_Kind: {
430 const VariableReference* v = (VariableReference*) expr;
431 if (v->fRefKind != VariableReference::kRead_RefKind) {
432 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400433 v,
434 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
435 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400436 }
437 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500438 default:
439 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700440 }
441 break;
442 }
443 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400444 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000445 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
446 VarDeclaration& vd = (VarDeclaration&) *stmt;
447 if (vd.fValue) {
448 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700449 }
450 }
451 break;
452 }
453 }
454}
455
456void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
457 BasicBlock& block = cfg->fBlocks[blockId];
458
459 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500460 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700461 for (const BasicBlock::Node& n : block.fNodes) {
462 this->addDefinitions(n, &after);
463 }
464
465 // propagate definitions to exits
466 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400467 if (exitId == blockId) {
468 continue;
469 }
ethannicholas22f939e2016-10-13 13:25:34 -0700470 BasicBlock& exit = cfg->fBlocks[exitId];
471 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500472 std::unique_ptr<Expression>* e1 = pair.second;
473 auto found = exit.fBefore.find(pair.first);
474 if (found == exit.fBefore.end()) {
475 // exit has no definition for it, just copy it
476 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700477 exit.fBefore[pair.first] = e1;
478 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500479 // exit has a (possibly different) value already defined
480 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700481 if (e1 != e2) {
482 // definition has changed, merge and add exit block to worklist
483 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500484 if (e1 && e2) {
485 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400486 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500487 } else {
488 exit.fBefore[pair.first] = nullptr;
489 }
ethannicholas22f939e2016-10-13 13:25:34 -0700490 }
491 }
492 }
493 }
494}
495
496// returns a map which maps all local variables in the function to null, indicating that their value
497// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500498static DefinitionMap compute_start_state(const CFG& cfg) {
499 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400500 for (const auto& block : cfg.fBlocks) {
501 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700502 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400503 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400504 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700505 if (s->fKind == Statement::kVarDeclarations_Kind) {
506 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000507 for (const auto& decl : vd->fDeclaration->fVars) {
508 if (decl->fKind == Statement::kVarDeclaration_Kind) {
509 result[((VarDeclaration&) *decl).fVar] = nullptr;
510 }
Mike Klein6ad99092016-10-26 10:35:22 -0400511 }
ethannicholas22f939e2016-10-13 13:25:34 -0700512 }
513 }
514 }
515 }
516 return result;
517}
518
Ethan Nicholascb670962017-04-20 19:31:52 -0400519/**
520 * Returns true if assigning to this lvalue has no effect.
521 */
522static bool is_dead(const Expression& lvalue) {
523 switch (lvalue.fKind) {
524 case Expression::kVariableReference_Kind:
525 return ((VariableReference&) lvalue).fVariable.dead();
526 case Expression::kSwizzle_Kind:
527 return is_dead(*((Swizzle&) lvalue).fBase);
528 case Expression::kFieldAccess_Kind:
529 return is_dead(*((FieldAccess&) lvalue).fBase);
530 case Expression::kIndex_Kind: {
531 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500532 return is_dead(*idx.fBase) &&
533 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400534 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500535 case Expression::kTernary_Kind: {
536 const TernaryExpression& t = (TernaryExpression&) lvalue;
537 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
538 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400539 case Expression::kExternalValue_Kind:
540 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400541 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500542#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400543 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500544#endif
545 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400546 }
547}
ethannicholas22f939e2016-10-13 13:25:34 -0700548
Ethan Nicholascb670962017-04-20 19:31:52 -0400549/**
550 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
551 * to a dead target and lack of side effects on the left hand side.
552 */
553static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700554 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400555 return false;
556 }
557 return is_dead(*b.fLeft);
558}
559
560void Compiler::computeDataFlow(CFG* cfg) {
561 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700562 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400563 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700564 workList.insert(i);
565 }
566 while (workList.size()) {
567 BlockId next = *workList.begin();
568 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400569 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700570 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400571}
572
573/**
574 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
575 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
576 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
577 * need to be regenerated).
578 */
579bool try_replace_expression(BasicBlock* b,
580 std::vector<BasicBlock::Node>::iterator* iter,
581 std::unique_ptr<Expression>* newExpression) {
582 std::unique_ptr<Expression>* target = (*iter)->expression();
583 if (!b->tryRemoveExpression(iter)) {
584 *target = std::move(*newExpression);
585 return false;
586 }
587 *target = std::move(*newExpression);
588 return b->tryInsertExpression(iter, target);
589}
590
591/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400592 * Returns true if the expression is a constant numeric literal with the specified value, or a
593 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400594 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400595bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 switch (expr.fKind) {
597 case Expression::kIntLiteral_Kind:
598 return ((IntLiteral&) expr).fValue == value;
599 case Expression::kFloatLiteral_Kind:
600 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400601 case Expression::kConstructor_Kind: {
602 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400603 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
604 : c.fType.isFloat();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400605 if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) {
606 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400607 if (isFloat) {
608 if (c.getFVecComponent(i) != value) {
609 return false;
610 }
611 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400612 return false;
613 }
614 }
615 return true;
616 }
617 return false;
618 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400619 default:
620 return false;
621 }
622}
623
624/**
625 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
626 * and CFG structures).
627 */
628void delete_left(BasicBlock* b,
629 std::vector<BasicBlock::Node>::iterator* iter,
630 bool* outUpdated,
631 bool* outNeedsRescan) {
632 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400633 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400634 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400635 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400636 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400637 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400638 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400639 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400640 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400641 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400642 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400643 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400644 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400645 *outNeedsRescan = true;
646 return;
647 }
648 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400649 *outNeedsRescan = true;
650 return;
651 }
652 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400653 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
654 (*iter)->expression() != &bin.fRight) {
655 *outNeedsRescan = true;
656 return;
657 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400658 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400659 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400660}
661
662/**
663 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
664 * CFG structures).
665 */
666void delete_right(BasicBlock* b,
667 std::vector<BasicBlock::Node>::iterator* iter,
668 bool* outUpdated,
669 bool* outNeedsRescan) {
670 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400671 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400672 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400673 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400674 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400675 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
676 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400677 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400678 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400679 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400680 *target = std::move(bin.fLeft);
681 if (*iter == b->fNodes.begin()) {
682 *outNeedsRescan = true;
683 return;
684 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400685 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400686 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
687 (*iter)->expression() != &bin.fLeft)) {
688 *outNeedsRescan = true;
689 return;
690 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400691 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400692 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400693}
694
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400695/**
696 * Constructs the specified type using a single argument.
697 */
698static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
699 std::vector<std::unique_ptr<Expression>> args;
700 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700701 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400702 return result;
703}
704
705/**
706 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
707 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
708 */
709static void vectorize(BasicBlock* b,
710 std::vector<BasicBlock::Node>::iterator* iter,
711 const Type& type,
712 std::unique_ptr<Expression>* otherExpression,
713 bool* outUpdated,
714 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400715 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
716 SkASSERT(type.kind() == Type::kVector_Kind);
717 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400718 *outUpdated = true;
719 std::unique_ptr<Expression>* target = (*iter)->expression();
720 if (!b->tryRemoveExpression(iter)) {
721 *target = construct(type, std::move(*otherExpression));
722 *outNeedsRescan = true;
723 } else {
724 *target = construct(type, std::move(*otherExpression));
725 if (!b->tryInsertExpression(iter, target)) {
726 *outNeedsRescan = true;
727 }
728 }
729}
730
731/**
732 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
733 * left to yield vec<n>(x).
734 */
735static void vectorize_left(BasicBlock* b,
736 std::vector<BasicBlock::Node>::iterator* iter,
737 bool* outUpdated,
738 bool* outNeedsRescan) {
739 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
740 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
741}
742
743/**
744 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
745 * right to yield vec<n>(y).
746 */
747static void vectorize_right(BasicBlock* b,
748 std::vector<BasicBlock::Node>::iterator* iter,
749 bool* outUpdated,
750 bool* outNeedsRescan) {
751 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
752 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
753}
754
755// Mark that an expression which we were writing to is no longer being written to
756void clear_write(const Expression& expr) {
757 switch (expr.fKind) {
758 case Expression::kVariableReference_Kind: {
759 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
760 break;
761 }
762 case Expression::kFieldAccess_Kind:
763 clear_write(*((FieldAccess&) expr).fBase);
764 break;
765 case Expression::kSwizzle_Kind:
766 clear_write(*((Swizzle&) expr).fBase);
767 break;
768 case Expression::kIndex_Kind:
769 clear_write(*((IndexExpression&) expr).fBase);
770 break;
771 default:
772 ABORT("shouldn't be writing to this kind of expression\n");
773 break;
774 }
775}
776
Ethan Nicholascb670962017-04-20 19:31:52 -0400777void Compiler::simplifyExpression(DefinitionMap& definitions,
778 BasicBlock& b,
779 std::vector<BasicBlock::Node>::iterator* iter,
780 std::unordered_set<const Variable*>* undefinedVariables,
781 bool* outUpdated,
782 bool* outNeedsRescan) {
783 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400784 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400785 if ((*iter)->fConstantPropagation) {
786 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
787 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400788 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400789 if (!try_replace_expression(&b, iter, &optimized)) {
790 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400791 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400792 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400793 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400794 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400795 }
796 }
797 switch (expr->fKind) {
798 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400799 const VariableReference& ref = (VariableReference&) *expr;
800 const Variable& var = ref.fVariable;
801 if (ref.refKind() != VariableReference::kWrite_RefKind &&
802 ref.refKind() != VariableReference::kPointer_RefKind &&
803 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400804 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
805 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000806 this->error(expr->fOffset,
807 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400808 }
809 break;
810 }
811 case Expression::kTernary_Kind: {
812 TernaryExpression* t = (TernaryExpression*) expr;
813 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
814 // ternary has a constant test, replace it with either the true or
815 // false branch
816 if (((BoolLiteral&) *t->fTest).fValue) {
817 (*iter)->setExpression(std::move(t->fIfTrue));
818 } else {
819 (*iter)->setExpression(std::move(t->fIfFalse));
820 }
821 *outUpdated = true;
822 *outNeedsRescan = true;
823 }
824 break;
825 }
826 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400827 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400828 if (dead_assignment(*bin)) {
829 delete_left(&b, iter, outUpdated, outNeedsRescan);
830 break;
831 }
832 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400833 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
834 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
835 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
836 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
837 break;
838 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400839 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400840 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400841 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400842 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
843 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400844 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400845 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
846 } else {
847 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400848 // 1 * float4(x) -> float4(x)
849 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400850 delete_left(&b, iter, outUpdated, outNeedsRescan);
851 }
852 }
853 else if (is_constant(*bin->fLeft, 0)) {
854 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500855 bin->fRight->fType.kind() == Type::kVector_Kind &&
856 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400857 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400858 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
859 } else {
860 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400861 // float4(0) * x -> float4(0)
862 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500863 if (!bin->fRight->hasSideEffects()) {
864 delete_right(&b, iter, outUpdated, outNeedsRescan);
865 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400866 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400867 }
868 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400869 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
870 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400871 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400872 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
873 } else {
874 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400875 // float4(x) * 1 -> float4(x)
876 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400877 delete_right(&b, iter, outUpdated, outNeedsRescan);
878 }
879 }
880 else if (is_constant(*bin->fRight, 0)) {
881 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500882 bin->fRight->fType.kind() == Type::kScalar_Kind &&
883 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400884 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400885 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
886 } else {
887 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400888 // x * float4(0) -> float4(0)
889 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500890 if (!bin->fLeft->hasSideEffects()) {
891 delete_left(&b, iter, outUpdated, outNeedsRescan);
892 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400893 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 }
895 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400896 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400897 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400898 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
899 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400900 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400901 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
902 } else {
903 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400904 // 0 + float4(x) -> float4(x)
905 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400906 delete_left(&b, iter, outUpdated, outNeedsRescan);
907 }
908 } else if (is_constant(*bin->fRight, 0)) {
909 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
910 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400911 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400912 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
913 } else {
914 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400915 // float4(x) + 0 -> float4(x)
916 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400917 delete_right(&b, iter, outUpdated, outNeedsRescan);
918 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400919 }
920 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400921 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400922 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400923 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
924 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400925 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400926 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
927 } else {
928 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400929 // float4(x) - 0 -> float4(x)
930 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400931 delete_right(&b, iter, outUpdated, outNeedsRescan);
932 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400933 }
934 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400935 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -0400936 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400937 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
938 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400939 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400940 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
941 } else {
942 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400943 // float4(x) / 1 -> float4(x)
944 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400945 delete_right(&b, iter, outUpdated, outNeedsRescan);
946 }
947 } else if (is_constant(*bin->fLeft, 0)) {
948 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500949 bin->fRight->fType.kind() == Type::kVector_Kind &&
950 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400951 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400952 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
953 } else {
954 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400955 // float4(0) / x -> float4(0)
956 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500957 if (!bin->fRight->hasSideEffects()) {
958 delete_right(&b, iter, outUpdated, outNeedsRescan);
959 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400960 }
961 }
962 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400963 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400964 if (is_constant(*bin->fRight, 0)) {
965 clear_write(*bin->fLeft);
966 delete_right(&b, iter, outUpdated, outNeedsRescan);
967 }
968 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400969 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400970 if (is_constant(*bin->fRight, 0)) {
971 clear_write(*bin->fLeft);
972 delete_right(&b, iter, outUpdated, outNeedsRescan);
973 }
974 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400975 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400976 if (is_constant(*bin->fRight, 1)) {
977 clear_write(*bin->fLeft);
978 delete_right(&b, iter, outUpdated, outNeedsRescan);
979 }
980 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400981 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400982 if (is_constant(*bin->fRight, 1)) {
983 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400984 delete_right(&b, iter, outUpdated, outNeedsRescan);
985 }
986 break;
987 default:
988 break;
989 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -0400990 break;
991 }
992 case Expression::kSwizzle_Kind: {
993 Swizzle& s = (Swizzle&) *expr;
994 // detect identity swizzles like foo.rgba
995 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
996 bool identity = true;
997 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
998 if (s.fComponents[i] != i) {
999 identity = false;
1000 break;
1001 }
1002 }
1003 if (identity) {
1004 *outUpdated = true;
1005 if (!try_replace_expression(&b, iter, &s.fBase)) {
1006 *outNeedsRescan = true;
1007 return;
1008 }
1009 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1010 break;
1011 }
1012 }
1013 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1014 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1015 Swizzle& base = (Swizzle&) *s.fBase;
1016 std::vector<int> final;
1017 for (int c : s.fComponents) {
1018 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1019 final.push_back(c);
1020 } else {
1021 final.push_back(base.fComponents[c]);
1022 }
1023 }
1024 *outUpdated = true;
1025 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1026 std::move(final)));
1027 if (!try_replace_expression(&b, iter, &replacement)) {
1028 *outNeedsRescan = true;
1029 return;
1030 }
1031 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1032 break;
1033 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001034 }
1035 default:
1036 break;
1037 }
1038}
1039
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001040// returns true if this statement could potentially execute a break at the current level (we ignore
1041// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
Ethan Nicholas5005a222018-08-24 13:06:27 -04001042static bool contains_conditional_break(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001043 switch (s.fKind) {
1044 case Statement::kBlock_Kind:
1045 for (const auto& sub : ((Block&) s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001046 if (contains_conditional_break(*sub, inConditional)) {
1047 return true;
1048 }
1049 }
1050 return false;
1051 case Statement::kBreak_Kind:
1052 return inConditional;
1053 case Statement::kIf_Kind: {
1054 const IfStatement& i = (IfStatement&) s;
1055 return contains_conditional_break(*i.fIfTrue, true) ||
1056 (i.fIfFalse && contains_conditional_break(*i.fIfFalse, true));
1057 }
1058 default:
1059 return false;
1060 }
1061}
1062
1063// returns true if this statement definitely executes a break at the current level (we ignore
1064// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1065static bool contains_unconditional_break(Statement& s) {
1066 switch (s.fKind) {
1067 case Statement::kBlock_Kind:
1068 for (const auto& sub : ((Block&) s).fStatements) {
1069 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001070 return true;
1071 }
1072 }
1073 return false;
1074 case Statement::kBreak_Kind:
1075 return true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001076 default:
1077 return false;
1078 }
1079}
1080
1081// Returns a block containing all of the statements that will be run if the given case matches
1082// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1083// broken by this call and must then be discarded).
1084// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1085// when break statements appear inside conditionals.
1086static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) {
1087 bool capturing = false;
1088 std::vector<std::unique_ptr<Statement>*> statementPtrs;
1089 for (const auto& current : s->fCases) {
1090 if (current.get() == c) {
1091 capturing = true;
1092 }
1093 if (capturing) {
1094 for (auto& stmt : current->fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001095 if (contains_conditional_break(*stmt, s->fKind == Statement::kIf_Kind)) {
1096 return nullptr;
1097 }
1098 if (contains_unconditional_break(*stmt)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001099 capturing = false;
1100 break;
1101 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001102 statementPtrs.push_back(&stmt);
1103 }
1104 if (!capturing) {
1105 break;
1106 }
1107 }
1108 }
1109 std::vector<std::unique_ptr<Statement>> statements;
1110 for (const auto& s : statementPtrs) {
1111 statements.push_back(std::move(*s));
1112 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001113 return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001114}
1115
Ethan Nicholascb670962017-04-20 19:31:52 -04001116void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001117 BasicBlock& b,
1118 std::vector<BasicBlock::Node>::iterator* iter,
1119 std::unordered_set<const Variable*>* undefinedVariables,
1120 bool* outUpdated,
1121 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001122 Statement* stmt = (*iter)->statement()->get();
1123 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001124 case Statement::kVarDeclaration_Kind: {
1125 const auto& varDecl = (VarDeclaration&) *stmt;
1126 if (varDecl.fVar->dead() &&
1127 (!varDecl.fValue ||
1128 !varDecl.fValue->hasSideEffects())) {
1129 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001130 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001131 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1132 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001133 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001134 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001135 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001136 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001137 }
1138 break;
1139 }
1140 case Statement::kIf_Kind: {
1141 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001142 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1143 // constant if, collapse down to a single branch
1144 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001145 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001146 (*iter)->setStatement(std::move(i.fIfTrue));
1147 } else {
1148 if (i.fIfFalse) {
1149 (*iter)->setStatement(std::move(i.fIfFalse));
1150 } else {
1151 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1152 }
1153 }
1154 *outUpdated = true;
1155 *outNeedsRescan = true;
1156 break;
1157 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001158 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1159 // else block doesn't do anything, remove it
1160 i.fIfFalse.reset();
1161 *outUpdated = true;
1162 *outNeedsRescan = true;
1163 }
1164 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1165 // if block doesn't do anything, no else block
1166 if (i.fTest->hasSideEffects()) {
1167 // test has side effects, keep it
1168 (*iter)->setStatement(std::unique_ptr<Statement>(
1169 new ExpressionStatement(std::move(i.fTest))));
1170 } else {
1171 // no if, no else, no test side effects, kill the whole if
1172 // statement
1173 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1174 }
1175 *outUpdated = true;
1176 *outNeedsRescan = true;
1177 }
1178 break;
1179 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001180 case Statement::kSwitch_Kind: {
1181 SwitchStatement& s = (SwitchStatement&) *stmt;
1182 if (s.fValue->isConstant()) {
1183 // switch is constant, replace it with the case that matches
1184 bool found = false;
1185 SwitchCase* defaultCase = nullptr;
1186 for (const auto& c : s.fCases) {
1187 if (!c->fValue) {
1188 defaultCase = c.get();
1189 continue;
1190 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001191 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001192 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001193 if (found) {
1194 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1195 if (newBlock) {
1196 (*iter)->setStatement(std::move(newBlock));
1197 break;
1198 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001199 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001200 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001201 "static switch contains non-static conditional break");
1202 s.fIsStatic = false;
1203 }
1204 return; // can't simplify
1205 }
1206 }
1207 }
1208 if (!found) {
1209 // no matching case. use default if it exists, or kill the whole thing
1210 if (defaultCase) {
1211 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1212 if (newBlock) {
1213 (*iter)->setStatement(std::move(newBlock));
1214 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001215 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001216 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001217 "static switch contains non-static conditional break");
1218 s.fIsStatic = false;
1219 }
1220 return; // can't simplify
1221 }
1222 } else {
1223 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1224 }
1225 }
1226 *outUpdated = true;
1227 *outNeedsRescan = true;
1228 }
1229 break;
1230 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001231 case Statement::kExpression_Kind: {
1232 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001233 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001234 if (!e.fExpression->hasSideEffects()) {
1235 // Expression statement with no side effects, kill it
1236 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1237 *outNeedsRescan = true;
1238 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001239 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001240 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1241 *outUpdated = true;
1242 }
1243 break;
1244 }
1245 default:
1246 break;
1247 }
1248}
1249
1250void Compiler::scanCFG(FunctionDefinition& f) {
1251 CFG cfg = CFGGenerator().getCFG(f);
1252 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001253
1254 // check for unreachable code
1255 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001256 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001257 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001258 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001259 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1260 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001261 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001262 break;
1263 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001264 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001265 break;
1266 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001267 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001268 }
1269 }
1270 if (fErrorCount) {
1271 return;
1272 }
1273
Ethan Nicholascb670962017-04-20 19:31:52 -04001274 // check for dead code & undefined variables, perform constant propagation
1275 std::unordered_set<const Variable*> undefinedVariables;
1276 bool updated;
1277 bool needsRescan = false;
1278 do {
1279 if (needsRescan) {
1280 cfg = CFGGenerator().getCFG(f);
1281 this->computeDataFlow(&cfg);
1282 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001283 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001284
1285 updated = false;
1286 for (BasicBlock& b : cfg.fBlocks) {
1287 DefinitionMap definitions = b.fBefore;
1288
1289 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1290 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1291 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1292 &needsRescan);
1293 } else {
1294 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1295 &needsRescan);
1296 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001297 if (needsRescan) {
1298 break;
1299 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001300 this->addDefinitions(*iter, &definitions);
1301 }
1302 }
1303 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001304 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001305
Ethan Nicholas91a10532017-06-22 11:24:38 -04001306 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001307 for (BasicBlock& b : cfg.fBlocks) {
1308 DefinitionMap definitions = b.fBefore;
1309
Ethan Nicholas91a10532017-06-22 11:24:38 -04001310 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001311 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1312 const Statement& s = **iter->statement();
1313 switch (s.fKind) {
1314 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001315 if (((const IfStatement&) s).fIsStatic &&
1316 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001317 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001318 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001319 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001320 break;
1321 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001322 if (((const SwitchStatement&) s).fIsStatic &&
1323 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001324 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001325 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001326 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001327 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001328 case Statement::kVarDeclarations_Kind: {
1329 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1330 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1331 if ((*varIter)->fKind == Statement::kNop_Kind) {
1332 varIter = decls.fVars.erase(varIter);
1333 } else {
1334 ++varIter;
1335 }
1336 }
1337 if (!decls.fVars.size()) {
1338 iter = b.fNodes.erase(iter);
1339 } else {
1340 ++iter;
1341 }
1342 break;
1343 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001344 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001345 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001346 break;
1347 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001348 } else {
1349 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001350 }
1351 }
1352 }
1353
ethannicholas22f939e2016-10-13 13:25:34 -07001354 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001355 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001356 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001357 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1358 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001359 }
1360 }
1361}
1362
Ethan Nicholas91164d12019-05-15 15:29:54 -04001363void Compiler::registerExternalValue(ExternalValue* value) {
1364 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1365}
1366
1367Symbol* Compiler::takeOwnership(std::unique_ptr<Symbol> symbol) {
1368 return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol));
1369}
1370
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001371std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001372 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001373 fErrorText = "";
1374 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001375 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001376 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001377 switch (kind) {
1378 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001379 inherited = &fVertexInclude;
1380 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001381 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001382 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001383 break;
1384 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001385 inherited = &fFragmentInclude;
1386 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001387 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001388 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001389 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001390 case Program::kGeometry_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001391 inherited = &fGeometryInclude;
1392 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001393 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001394 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001395 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001396 case Program::kFragmentProcessor_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001397 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001398 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001399 fIRGenerator->start(&settings, nullptr);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001400 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001401 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes,
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001402 &elements);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001403 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001404 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001405 case Program::kPipelineStage_Kind:
1406 inherited = &fPipelineInclude;
1407 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001408 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001409 fIRGenerator->start(&settings, inherited);
1410 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001411 case Program::kGeneric_Kind:
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001412 inherited = &fInterpreterInclude;
1413 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001414 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001415 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001416 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001417 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001418 for (auto& element : elements) {
1419 if (element->fKind == ProgramElement::kEnum_Kind) {
1420 ((Enum&) *element).fBuiltin = true;
1421 }
1422 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001423 std::unique_ptr<String> textPtr(new String(std::move(text)));
1424 fSource = textPtr.get();
Robert Phillipsfe8da172018-01-24 14:52:02 +00001425 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001426 auto result = std::unique_ptr<Program>(new Program(kind,
1427 std::move(textPtr),
1428 settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001429 fContext,
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001430 inherited,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001431 std::move(elements),
1432 fIRGenerator->fSymbolTable,
1433 fIRGenerator->fInputs));
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001434 if (fErrorCount) {
1435 return nullptr;
1436 }
1437 return result;
1438}
1439
Ethan Nicholas00543112018-07-31 09:44:36 -04001440bool Compiler::optimize(Program& program) {
1441 SkASSERT(!fErrorCount);
1442 if (!program.fIsOptimized) {
1443 program.fIsOptimized = true;
1444 fIRGenerator->fKind = program.fKind;
1445 fIRGenerator->fSettings = &program.fSettings;
1446 for (auto& element : program) {
1447 if (element.fKind == ProgramElement::kFunction_Kind) {
1448 this->scanCFG((FunctionDefinition&) element);
1449 }
1450 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001451 if (program.fKind != Program::kFragmentProcessor_Kind) {
1452 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1453 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1454 VarDeclarations& vars = (VarDeclarations&) **iter;
1455 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1456 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1457 if (var.dead()) {
1458 varIter = vars.fVars.erase(varIter);
1459 } else {
1460 ++varIter;
1461 }
1462 }
1463 if (vars.fVars.size() == 0) {
1464 iter = program.fElements.erase(iter);
1465 continue;
1466 }
1467 }
1468 ++iter;
1469 }
1470 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001471 }
1472 return fErrorCount == 0;
1473}
1474
1475std::unique_ptr<Program> Compiler::specialize(
1476 Program& program,
1477 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1478 std::vector<std::unique_ptr<ProgramElement>> elements;
1479 for (const auto& e : program) {
1480 elements.push_back(e.clone());
1481 }
1482 Program::Settings settings;
1483 settings.fCaps = program.fSettings.fCaps;
1484 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1485 settings.fArgs.insert(*iter);
1486 }
Brian Osman808f0212020-01-21 15:36:47 -05001487 std::unique_ptr<String> sourceCopy(new String(*program.fSource));
Ethan Nicholas00543112018-07-31 09:44:36 -04001488 std::unique_ptr<Program> result(new Program(program.fKind,
Brian Osman808f0212020-01-21 15:36:47 -05001489 std::move(sourceCopy),
Ethan Nicholas00543112018-07-31 09:44:36 -04001490 settings,
1491 program.fContext,
1492 program.fInheritedElements,
1493 std::move(elements),
1494 program.fSymbols,
1495 program.fInputs));
1496 return result;
1497}
1498
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001499#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1500
Ethan Nicholas00543112018-07-31 09:44:36 -04001501bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1502 if (!this->optimize(program)) {
1503 return false;
1504 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001505#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001506 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001507 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001508 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001509 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001510 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001511 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001512 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001513 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001514 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001515 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1516 SkDebugf("SPIR-V validation error: %s\n", m);
1517 };
1518 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001519 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001520 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001521 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001522 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001523 }
1524#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001525 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001526 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001527 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001528 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001529#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001530 return result;
1531}
1532
Ethan Nicholas00543112018-07-31 09:44:36 -04001533bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001534 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001535 bool result = this->toSPIRV(program, buffer);
1536 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001537 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001538 }
1539 return result;
1540}
1541
Ethan Nicholas00543112018-07-31 09:44:36 -04001542bool Compiler::toGLSL(Program& program, OutputStream& out) {
1543 if (!this->optimize(program)) {
1544 return false;
1545 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001546 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001547 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001548 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001549 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001550 return result;
1551}
1552
Ethan Nicholas00543112018-07-31 09:44:36 -04001553bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001554 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001555 bool result = this->toGLSL(program, buffer);
1556 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001557 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001558 }
1559 return result;
1560}
1561
Brian Osmanc0243912020-02-19 15:35:26 -05001562bool Compiler::toHLSL(Program& program, String* out) {
1563 String spirv;
1564 if (!this->toSPIRV(program, &spirv)) {
1565 return false;
1566 }
1567
1568 return SPIRVtoHLSL(spirv, out);
1569}
1570
Ethan Nicholas00543112018-07-31 09:44:36 -04001571bool Compiler::toMetal(Program& program, OutputStream& out) {
1572 if (!this->optimize(program)) {
1573 return false;
1574 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001575 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001576 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001577 return result;
1578}
1579
Ethan Nicholas00543112018-07-31 09:44:36 -04001580bool Compiler::toMetal(Program& program, String* out) {
1581 if (!this->optimize(program)) {
1582 return false;
1583 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001584 StringStream buffer;
1585 bool result = this->toMetal(program, buffer);
1586 if (result) {
1587 *out = buffer.str();
1588 }
1589 return result;
1590}
1591
Ethan Nicholas00543112018-07-31 09:44:36 -04001592bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1593 if (!this->optimize(program)) {
1594 return false;
1595 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001596 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001597 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001598 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001599 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001600 return result;
1601}
1602
Ethan Nicholas00543112018-07-31 09:44:36 -04001603bool Compiler::toH(Program& program, String name, OutputStream& out) {
1604 if (!this->optimize(program)) {
1605 return false;
1606 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001607 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001608 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001609 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001610 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001611 return result;
1612}
1613
Brian Osman2e29ab52019-09-20 12:19:11 -04001614#endif
1615
1616#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osman107c6662019-12-30 15:02:30 -05001617bool Compiler::toPipelineStage(const Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001618 SkASSERT(program.fIsOptimized);
1619 fSource = program.fSource.get();
1620 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001621 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001622 bool result = cg.generateCode();
1623 fSource = nullptr;
1624 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001625 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001626 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001627 return result;
1628}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001629#endif
1630
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001631std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001632#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001633 if (!this->optimize(program)) {
1634 return nullptr;
1635 }
Brian Osman808f0212020-01-21 15:36:47 -05001636 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001637 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001638 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1639 bool success = cg.generateCode();
1640 fSource = nullptr;
1641 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001642 return result;
1643 }
Brian Osman489cf882019-07-09 10:48:28 -04001644#else
1645 ABORT("ByteCode interpreter not enabled");
1646#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001647 return nullptr;
1648}
1649
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001650const char* Compiler::OperatorName(Token::Kind kind) {
1651 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001652 case Token::Kind::TK_PLUS: return "+";
1653 case Token::Kind::TK_MINUS: return "-";
1654 case Token::Kind::TK_STAR: return "*";
1655 case Token::Kind::TK_SLASH: return "/";
1656 case Token::Kind::TK_PERCENT: return "%";
1657 case Token::Kind::TK_SHL: return "<<";
1658 case Token::Kind::TK_SHR: return ">>";
1659 case Token::Kind::TK_LOGICALNOT: return "!";
1660 case Token::Kind::TK_LOGICALAND: return "&&";
1661 case Token::Kind::TK_LOGICALOR: return "||";
1662 case Token::Kind::TK_LOGICALXOR: return "^^";
1663 case Token::Kind::TK_BITWISENOT: return "~";
1664 case Token::Kind::TK_BITWISEAND: return "&";
1665 case Token::Kind::TK_BITWISEOR: return "|";
1666 case Token::Kind::TK_BITWISEXOR: return "^";
1667 case Token::Kind::TK_EQ: return "=";
1668 case Token::Kind::TK_EQEQ: return "==";
1669 case Token::Kind::TK_NEQ: return "!=";
1670 case Token::Kind::TK_LT: return "<";
1671 case Token::Kind::TK_GT: return ">";
1672 case Token::Kind::TK_LTEQ: return "<=";
1673 case Token::Kind::TK_GTEQ: return ">=";
1674 case Token::Kind::TK_PLUSEQ: return "+=";
1675 case Token::Kind::TK_MINUSEQ: return "-=";
1676 case Token::Kind::TK_STAREQ: return "*=";
1677 case Token::Kind::TK_SLASHEQ: return "/=";
1678 case Token::Kind::TK_PERCENTEQ: return "%=";
1679 case Token::Kind::TK_SHLEQ: return "<<=";
1680 case Token::Kind::TK_SHREQ: return ">>=";
1681 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1682 case Token::Kind::TK_LOGICALOREQ: return "||=";
1683 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1684 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1685 case Token::Kind::TK_BITWISEOREQ: return "|=";
1686 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1687 case Token::Kind::TK_PLUSPLUS: return "++";
1688 case Token::Kind::TK_MINUSMINUS: return "--";
1689 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001690 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001691 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001692 }
1693}
1694
1695
1696bool Compiler::IsAssignment(Token::Kind op) {
1697 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001698 case Token::Kind::TK_EQ: // fall through
1699 case Token::Kind::TK_PLUSEQ: // fall through
1700 case Token::Kind::TK_MINUSEQ: // fall through
1701 case Token::Kind::TK_STAREQ: // fall through
1702 case Token::Kind::TK_SLASHEQ: // fall through
1703 case Token::Kind::TK_PERCENTEQ: // fall through
1704 case Token::Kind::TK_SHLEQ: // fall through
1705 case Token::Kind::TK_SHREQ: // fall through
1706 case Token::Kind::TK_BITWISEOREQ: // fall through
1707 case Token::Kind::TK_BITWISEXOREQ: // fall through
1708 case Token::Kind::TK_BITWISEANDEQ: // fall through
1709 case Token::Kind::TK_LOGICALOREQ: // fall through
1710 case Token::Kind::TK_LOGICALXOREQ: // fall through
1711 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001712 return true;
1713 default:
1714 return false;
1715 }
1716}
1717
1718Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001719 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001720 int line = 1;
1721 int column = 1;
1722 for (int i = 0; i < offset; i++) {
1723 if ((*fSource)[i] == '\n') {
1724 ++line;
1725 column = 1;
1726 }
1727 else {
1728 ++column;
1729 }
1730 }
1731 return Position(line, column);
1732}
1733
1734void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001735 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001736 Position pos = this->position(offset);
1737 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001738}
1739
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001740String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001741 this->writeErrorCount();
1742 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001743 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001744 return result;
1745}
1746
1747void Compiler::writeErrorCount() {
1748 if (fErrorCount) {
1749 fErrorText += to_string(fErrorCount) + " error";
1750 if (fErrorCount > 1) {
1751 fErrorText += "s";
1752 }
1753 fErrorText += "\n";
1754 }
1755}
1756
ethannicholasb3058bd2016-07-01 08:22:01 -07001757} // namespace