blob: 31492bd493f3d0f31289299795b4895e8edcd54d [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLByteCodeGenerator.h"
11#include "src/sksl/SkSLCFGGenerator.h"
12#include "src/sksl/SkSLCPPCodeGenerator.h"
13#include "src/sksl/SkSLGLSLCodeGenerator.h"
14#include "src/sksl/SkSLHCodeGenerator.h"
15#include "src/sksl/SkSLIRGenerator.h"
16#include "src/sksl/SkSLMetalCodeGenerator.h"
17#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
18#include "src/sksl/SkSLSPIRVCodeGenerator.h"
19#include "src/sksl/ir/SkSLEnum.h"
20#include "src/sksl/ir/SkSLExpression.h"
21#include "src/sksl/ir/SkSLExpressionStatement.h"
22#include "src/sksl/ir/SkSLFunctionCall.h"
23#include "src/sksl/ir/SkSLIntLiteral.h"
24#include "src/sksl/ir/SkSLModifiersDeclaration.h"
25#include "src/sksl/ir/SkSLNop.h"
26#include "src/sksl/ir/SkSLSymbolTable.h"
27#include "src/sksl/ir/SkSLTernaryExpression.h"
28#include "src/sksl/ir/SkSLUnresolvedFunction.h"
29#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070030
Ethan Nicholasa11035b2019-11-26 16:27:47 -050031#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
32#include "include/gpu/GrContextOptions.h"
33#include "src/gpu/GrShaderCaps.h"
34#endif
35
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040036#ifdef SK_ENABLE_SPIRV_VALIDATION
37#include "spirv-tools/libspirv.hpp"
38#endif
39
ethannicholasb3058bd2016-07-01 08:22:01 -070040// include the built-in shader symbols as static strings
41
Ethan Nicholas79707652017-11-16 11:20:11 -050042#define STRINGIFY(x) #x
43
Ethan Nicholas8da1e652019-05-24 11:01:59 -040044static const char* SKSL_GPU_INCLUDE =
45#include "sksl_gpu.inc"
46;
47
48static const char* SKSL_INTERP_INCLUDE =
49#include "sksl_interp.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070050;
51
ethannicholas5961bc92016-10-12 06:39:56 -070052static const char* SKSL_VERT_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050053#include "sksl_vert.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070054;
55
ethannicholas5961bc92016-10-12 06:39:56 -070056static const char* SKSL_FRAG_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050057#include "sksl_frag.inc"
ethannicholasb3058bd2016-07-01 08:22:01 -070058;
59
Ethan Nicholas52cad152017-02-16 16:37:32 -050060static const char* SKSL_GEOM_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050061#include "sksl_geom.inc"
Ethan Nicholas52cad152017-02-16 16:37:32 -050062;
63
Ethan Nicholas762466e2017-06-29 10:03:38 -040064static const char* SKSL_FP_INCLUDE =
Ben Wagnera56c4d22018-01-24 17:32:17 -050065#include "sksl_enums.inc"
66#include "sksl_fp.inc"
Ethan Nicholas762466e2017-06-29 10:03:38 -040067;
68
Ethan Nicholas8da1e652019-05-24 11:01:59 -040069static const char* SKSL_PIPELINE_INCLUDE =
70#include "sksl_pipeline.inc"
Ethan Nicholas0d997662019-04-08 09:46:01 -040071;
72
ethannicholasb3058bd2016-07-01 08:22:01 -070073namespace SkSL {
74
Ethan Nicholasdb80f692019-11-22 14:06:12 -050075static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
76 std::map<StringFragment, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
77 for (auto& element : *src) {
78 switch (element->fKind) {
79 case ProgramElement::kFunction_Kind: {
80 FunctionDefinition& f = (FunctionDefinition&) *element;
81 StringFragment name = f.fDeclaration.fName;
82 SkASSERT(target->find(name) == target->end());
83 (*target)[name] = std::make_pair(std::move(element), false);
84 break;
85 }
86 case ProgramElement::kEnum_Kind: {
87 Enum& e = (Enum&) *element;
88 StringFragment name = e.fTypeName;
89 SkASSERT(target->find(name) == target->end());
90 (*target)[name] = std::make_pair(std::move(element), false);
91 break;
92 }
93 default:
94 printf("unsupported include file element\n");
95 SkASSERT(false);
96 }
97 }
98}
99
100
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400101Compiler::Compiler(Flags flags)
102: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400103, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400104, fErrorCount(0) {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400105 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
106 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400107 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 fTypes = types;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400109 #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
110 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700111 ADD_TYPE(Void);
112 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400113 ADD_TYPE(Float2);
114 ADD_TYPE(Float3);
115 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400116 ADD_TYPE(Half);
117 ADD_TYPE(Half2);
118 ADD_TYPE(Half3);
119 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 ADD_TYPE(Double);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400121 ADD_TYPE(Double2);
122 ADD_TYPE(Double3);
123 ADD_TYPE(Double4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700124 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400125 ADD_TYPE(Int2);
126 ADD_TYPE(Int3);
127 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400129 ADD_TYPE(UInt2);
130 ADD_TYPE(UInt3);
131 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400132 ADD_TYPE(Short);
133 ADD_TYPE(Short2);
134 ADD_TYPE(Short3);
135 ADD_TYPE(Short4);
136 ADD_TYPE(UShort);
137 ADD_TYPE(UShort2);
138 ADD_TYPE(UShort3);
139 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400140 ADD_TYPE(Byte);
141 ADD_TYPE(Byte2);
142 ADD_TYPE(Byte3);
143 ADD_TYPE(Byte4);
144 ADD_TYPE(UByte);
145 ADD_TYPE(UByte2);
146 ADD_TYPE(UByte3);
147 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400149 ADD_TYPE(Bool2);
150 ADD_TYPE(Bool3);
151 ADD_TYPE(Bool4);
152 ADD_TYPE(Float2x2);
153 ADD_TYPE(Float2x3);
154 ADD_TYPE(Float2x4);
155 ADD_TYPE(Float3x2);
156 ADD_TYPE(Float3x3);
157 ADD_TYPE(Float3x4);
158 ADD_TYPE(Float4x2);
159 ADD_TYPE(Float4x3);
160 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400161 ADD_TYPE(Half2x2);
162 ADD_TYPE(Half2x3);
163 ADD_TYPE(Half2x4);
164 ADD_TYPE(Half3x2);
165 ADD_TYPE(Half3x3);
166 ADD_TYPE(Half3x4);
167 ADD_TYPE(Half4x2);
168 ADD_TYPE(Half4x3);
169 ADD_TYPE(Half4x4);
170 ADD_TYPE(Double2x2);
171 ADD_TYPE(Double2x3);
172 ADD_TYPE(Double2x4);
173 ADD_TYPE(Double3x2);
174 ADD_TYPE(Double3x3);
175 ADD_TYPE(Double3x4);
176 ADD_TYPE(Double4x2);
177 ADD_TYPE(Double4x3);
178 ADD_TYPE(Double4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700179 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400180 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181 ADD_TYPE(GenDType);
182 ADD_TYPE(GenIType);
183 ADD_TYPE(GenUType);
184 ADD_TYPE(GenBType);
185 ADD_TYPE(Mat);
186 ADD_TYPE(Vec);
187 ADD_TYPE(GVec);
188 ADD_TYPE(GVec2);
189 ADD_TYPE(GVec3);
190 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400191 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 ADD_TYPE(DVec);
193 ADD_TYPE(IVec);
194 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400195 ADD_TYPE(SVec);
196 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400197 ADD_TYPE(ByteVec);
198 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700199 ADD_TYPE(BVec);
200
201 ADD_TYPE(Sampler1D);
202 ADD_TYPE(Sampler2D);
203 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700204 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 ADD_TYPE(SamplerCube);
206 ADD_TYPE(Sampler2DRect);
207 ADD_TYPE(Sampler1DArray);
208 ADD_TYPE(Sampler2DArray);
209 ADD_TYPE(SamplerCubeArray);
210 ADD_TYPE(SamplerBuffer);
211 ADD_TYPE(Sampler2DMS);
212 ADD_TYPE(Sampler2DMSArray);
213
Brian Salomonbf7b6202016-11-11 16:08:03 -0500214 ADD_TYPE(ISampler2D);
215
Brian Salomon2a51de82016-11-16 12:06:01 -0500216 ADD_TYPE(Image2D);
217 ADD_TYPE(IImage2D);
218
Greg Daniel64773e62016-11-22 09:44:03 -0500219 ADD_TYPE(SubpassInput);
220 ADD_TYPE(SubpassInputMS);
221
ethannicholasb3058bd2016-07-01 08:22:01 -0700222 ADD_TYPE(GSampler1D);
223 ADD_TYPE(GSampler2D);
224 ADD_TYPE(GSampler3D);
225 ADD_TYPE(GSamplerCube);
226 ADD_TYPE(GSampler2DRect);
227 ADD_TYPE(GSampler1DArray);
228 ADD_TYPE(GSampler2DArray);
229 ADD_TYPE(GSamplerCubeArray);
230 ADD_TYPE(GSamplerBuffer);
231 ADD_TYPE(GSampler2DMS);
232 ADD_TYPE(GSampler2DMSArray);
233
234 ADD_TYPE(Sampler1DShadow);
235 ADD_TYPE(Sampler2DShadow);
236 ADD_TYPE(SamplerCubeShadow);
237 ADD_TYPE(Sampler2DRectShadow);
238 ADD_TYPE(Sampler1DArrayShadow);
239 ADD_TYPE(Sampler2DArrayShadow);
240 ADD_TYPE(SamplerCubeArrayShadow);
241 ADD_TYPE(GSampler2DArrayShadow);
242 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400243 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400244 ADD_TYPE(Sampler);
245 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700247 StringFragment skCapsName("sk_Caps");
248 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400249 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500250 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
251
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700252 StringFragment skArgsName("sk_Args");
253 Variable* skArgs = new Variable(-1, Modifiers(), skArgsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400254 *fContext->fSkArgs_Type, Variable::kGlobal_Storage);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400255 fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs));
256
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500257 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
258 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400259 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500260 symbols, &gpuIntrinsics, &fGpuSymbolTable);
261 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
262 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
263 // remain valid
264 fGpuIncludeSource = std::move(fIRGenerator->fFile);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400265 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
266 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
267 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
268 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
269 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, strlen(SKSL_GEOM_INCLUDE),
270 fGpuSymbolTable, &fGeometryInclude, &fGeometrySymbolTable);
271 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
272 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
273 &fPipelineSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500274 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400275 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
276 strlen(SKSL_INTERP_INCLUDE), symbols, &fInterpreterInclude,
277 &fInterpreterSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500278 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279}
280
281Compiler::~Compiler() {
282 delete fIRGenerator;
283}
284
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400285void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
286 std::shared_ptr<SymbolTable> base,
287 std::vector<std::unique_ptr<ProgramElement>>* outElements,
288 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500289#ifdef SK_DEBUG
290 String source(src, length);
291 fSource = &source;
292#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400293 fIRGenerator->fSymbolTable = std::move(base);
294 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500295#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
296 GrContextOptions opts;
297 GrShaderCaps caps(opts);
298 settings.fCaps = &caps;
299#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400300 fIRGenerator->start(&settings, nullptr);
301 fIRGenerator->convertProgram(kind, src, length, *fTypes, outElements);
302 if (this->fErrorCount) {
303 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
304 }
305 SkASSERT(!fErrorCount);
306 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
307 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500308#ifdef SK_DEBUG
309 fSource = nullptr;
310#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400311}
312
ethannicholas22f939e2016-10-13 13:25:34 -0700313// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500314void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
315 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700316 switch (lvalue->fKind) {
317 case Expression::kVariableReference_Kind: {
318 const Variable& var = ((VariableReference*) lvalue)->fVariable;
319 if (var.fStorage == Variable::kLocal_Storage) {
320 (*definitions)[&var] = expr;
321 }
322 break;
323 }
324 case Expression::kSwizzle_Kind:
325 // We consider the variable written to as long as at least some of its components have
326 // been written to. This will lead to some false negatives (we won't catch it if you
327 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400328 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
329 // 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 -0700330 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400331 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400332 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700333 definitions);
334 break;
335 case Expression::kIndex_Kind:
336 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400337 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400338 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700339 definitions);
340 break;
341 case Expression::kFieldAccess_Kind:
342 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400343 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400344 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700345 definitions);
346 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500347 case Expression::kTernary_Kind:
348 // To simplify analysis, we just pretend that we write to both sides of the ternary.
349 // This allows for false positives (meaning we fail to detect that a variable might not
350 // have been assigned), but is preferable to false negatives.
351 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400352 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500353 definitions);
354 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400355 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500356 definitions);
357 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400358 case Expression::kExternalValue_Kind:
359 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700360 default:
361 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400362 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700363 }
364}
365
366// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400367void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500368 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700369 switch (node.fKind) {
370 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400371 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400372 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500373 switch (expr->fKind) {
374 case Expression::kBinary_Kind: {
375 BinaryExpression* b = (BinaryExpression*) expr;
376 if (b->fOperator == Token::EQ) {
377 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700378 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500379 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400380 b->fLeft.get(),
381 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
382 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500383
384 }
385 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700386 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400387 case Expression::kFunctionCall_Kind: {
388 const FunctionCall& c = (const FunctionCall&) *expr;
389 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
390 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
391 this->addDefinition(
392 c.fArguments[i].get(),
393 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
394 definitions);
395 }
396 }
397 break;
398 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500399 case Expression::kPrefix_Kind: {
400 const PrefixExpression* p = (PrefixExpression*) expr;
401 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
402 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400403 p->fOperand.get(),
404 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
405 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500406 }
407 break;
408 }
409 case Expression::kPostfix_Kind: {
410 const PostfixExpression* p = (PostfixExpression*) expr;
411 if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
412 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400413 p->fOperand.get(),
414 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
415 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500416 }
417 break;
418 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400419 case Expression::kVariableReference_Kind: {
420 const VariableReference* v = (VariableReference*) expr;
421 if (v->fRefKind != VariableReference::kRead_RefKind) {
422 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400423 v,
424 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
425 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400426 }
427 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500428 default:
429 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700430 }
431 break;
432 }
433 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400434 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000435 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
436 VarDeclaration& vd = (VarDeclaration&) *stmt;
437 if (vd.fValue) {
438 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700439 }
440 }
441 break;
442 }
443 }
444}
445
446void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
447 BasicBlock& block = cfg->fBlocks[blockId];
448
449 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500450 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700451 for (const BasicBlock::Node& n : block.fNodes) {
452 this->addDefinitions(n, &after);
453 }
454
455 // propagate definitions to exits
456 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400457 if (exitId == blockId) {
458 continue;
459 }
ethannicholas22f939e2016-10-13 13:25:34 -0700460 BasicBlock& exit = cfg->fBlocks[exitId];
461 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500462 std::unique_ptr<Expression>* e1 = pair.second;
463 auto found = exit.fBefore.find(pair.first);
464 if (found == exit.fBefore.end()) {
465 // exit has no definition for it, just copy it
466 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700467 exit.fBefore[pair.first] = e1;
468 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500469 // exit has a (possibly different) value already defined
470 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700471 if (e1 != e2) {
472 // definition has changed, merge and add exit block to worklist
473 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500474 if (e1 && e2) {
475 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400476 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500477 } else {
478 exit.fBefore[pair.first] = nullptr;
479 }
ethannicholas22f939e2016-10-13 13:25:34 -0700480 }
481 }
482 }
483 }
484}
485
486// returns a map which maps all local variables in the function to null, indicating that their value
487// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500488static DefinitionMap compute_start_state(const CFG& cfg) {
489 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400490 for (const auto& block : cfg.fBlocks) {
491 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700492 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400493 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400494 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700495 if (s->fKind == Statement::kVarDeclarations_Kind) {
496 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000497 for (const auto& decl : vd->fDeclaration->fVars) {
498 if (decl->fKind == Statement::kVarDeclaration_Kind) {
499 result[((VarDeclaration&) *decl).fVar] = nullptr;
500 }
Mike Klein6ad99092016-10-26 10:35:22 -0400501 }
ethannicholas22f939e2016-10-13 13:25:34 -0700502 }
503 }
504 }
505 }
506 return result;
507}
508
Ethan Nicholascb670962017-04-20 19:31:52 -0400509/**
510 * Returns true if assigning to this lvalue has no effect.
511 */
512static bool is_dead(const Expression& lvalue) {
513 switch (lvalue.fKind) {
514 case Expression::kVariableReference_Kind:
515 return ((VariableReference&) lvalue).fVariable.dead();
516 case Expression::kSwizzle_Kind:
517 return is_dead(*((Swizzle&) lvalue).fBase);
518 case Expression::kFieldAccess_Kind:
519 return is_dead(*((FieldAccess&) lvalue).fBase);
520 case Expression::kIndex_Kind: {
521 const IndexExpression& idx = (IndexExpression&) lvalue;
522 return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects();
523 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500524 case Expression::kTernary_Kind: {
525 const TernaryExpression& t = (TernaryExpression&) lvalue;
526 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
527 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400528 case Expression::kExternalValue_Kind:
529 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400530 default:
531 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
532 }
533}
ethannicholas22f939e2016-10-13 13:25:34 -0700534
Ethan Nicholascb670962017-04-20 19:31:52 -0400535/**
536 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
537 * to a dead target and lack of side effects on the left hand side.
538 */
539static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700540 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400541 return false;
542 }
543 return is_dead(*b.fLeft);
544}
545
546void Compiler::computeDataFlow(CFG* cfg) {
547 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700548 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400549 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700550 workList.insert(i);
551 }
552 while (workList.size()) {
553 BlockId next = *workList.begin();
554 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400555 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700556 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400557}
558
559/**
560 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
561 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
562 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
563 * need to be regenerated).
564 */
565bool try_replace_expression(BasicBlock* b,
566 std::vector<BasicBlock::Node>::iterator* iter,
567 std::unique_ptr<Expression>* newExpression) {
568 std::unique_ptr<Expression>* target = (*iter)->expression();
569 if (!b->tryRemoveExpression(iter)) {
570 *target = std::move(*newExpression);
571 return false;
572 }
573 *target = std::move(*newExpression);
574 return b->tryInsertExpression(iter, target);
575}
576
577/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400578 * Returns true if the expression is a constant numeric literal with the specified value, or a
579 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400580 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400581bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400582 switch (expr.fKind) {
583 case Expression::kIntLiteral_Kind:
584 return ((IntLiteral&) expr).fValue == value;
585 case Expression::kFloatLiteral_Kind:
586 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400587 case Expression::kConstructor_Kind: {
588 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400589 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
590 : c.fType.isFloat();
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400591 if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) {
592 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400593 if (isFloat) {
594 if (c.getFVecComponent(i) != value) {
595 return false;
596 }
597 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400598 return false;
599 }
600 }
601 return true;
602 }
603 return false;
604 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400605 default:
606 return false;
607 }
608}
609
610/**
611 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
612 * and CFG structures).
613 */
614void delete_left(BasicBlock* b,
615 std::vector<BasicBlock::Node>::iterator* iter,
616 bool* outUpdated,
617 bool* outNeedsRescan) {
618 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400619 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400620 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400621 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400622 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400623 bool result;
624 if (bin.fOperator == Token::EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400625 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400626 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400627 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400628 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400629 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400630 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400631 *outNeedsRescan = true;
632 return;
633 }
634 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400635 *outNeedsRescan = true;
636 return;
637 }
638 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400639 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
640 (*iter)->expression() != &bin.fRight) {
641 *outNeedsRescan = true;
642 return;
643 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400644 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400645 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400646}
647
648/**
649 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
650 * CFG structures).
651 */
652void delete_right(BasicBlock* b,
653 std::vector<BasicBlock::Node>::iterator* iter,
654 bool* outUpdated,
655 bool* outNeedsRescan) {
656 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400657 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400658 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400659 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400660 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400661 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
662 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400663 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400664 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400665 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400666 *target = std::move(bin.fLeft);
667 if (*iter == b->fNodes.begin()) {
668 *outNeedsRescan = true;
669 return;
670 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400671 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400672 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
673 (*iter)->expression() != &bin.fLeft)) {
674 *outNeedsRescan = true;
675 return;
676 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400677 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400678 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400679}
680
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400681/**
682 * Constructs the specified type using a single argument.
683 */
684static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
685 std::vector<std::unique_ptr<Expression>> args;
686 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700687 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400688 return result;
689}
690
691/**
692 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
693 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
694 */
695static void vectorize(BasicBlock* b,
696 std::vector<BasicBlock::Node>::iterator* iter,
697 const Type& type,
698 std::unique_ptr<Expression>* otherExpression,
699 bool* outUpdated,
700 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400701 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
702 SkASSERT(type.kind() == Type::kVector_Kind);
703 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400704 *outUpdated = true;
705 std::unique_ptr<Expression>* target = (*iter)->expression();
706 if (!b->tryRemoveExpression(iter)) {
707 *target = construct(type, std::move(*otherExpression));
708 *outNeedsRescan = true;
709 } else {
710 *target = construct(type, std::move(*otherExpression));
711 if (!b->tryInsertExpression(iter, target)) {
712 *outNeedsRescan = true;
713 }
714 }
715}
716
717/**
718 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
719 * left to yield vec<n>(x).
720 */
721static void vectorize_left(BasicBlock* b,
722 std::vector<BasicBlock::Node>::iterator* iter,
723 bool* outUpdated,
724 bool* outNeedsRescan) {
725 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
726 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
727}
728
729/**
730 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
731 * right to yield vec<n>(y).
732 */
733static void vectorize_right(BasicBlock* b,
734 std::vector<BasicBlock::Node>::iterator* iter,
735 bool* outUpdated,
736 bool* outNeedsRescan) {
737 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
738 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
739}
740
741// Mark that an expression which we were writing to is no longer being written to
742void clear_write(const Expression& expr) {
743 switch (expr.fKind) {
744 case Expression::kVariableReference_Kind: {
745 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
746 break;
747 }
748 case Expression::kFieldAccess_Kind:
749 clear_write(*((FieldAccess&) expr).fBase);
750 break;
751 case Expression::kSwizzle_Kind:
752 clear_write(*((Swizzle&) expr).fBase);
753 break;
754 case Expression::kIndex_Kind:
755 clear_write(*((IndexExpression&) expr).fBase);
756 break;
757 default:
758 ABORT("shouldn't be writing to this kind of expression\n");
759 break;
760 }
761}
762
Ethan Nicholascb670962017-04-20 19:31:52 -0400763void Compiler::simplifyExpression(DefinitionMap& definitions,
764 BasicBlock& b,
765 std::vector<BasicBlock::Node>::iterator* iter,
766 std::unordered_set<const Variable*>* undefinedVariables,
767 bool* outUpdated,
768 bool* outNeedsRescan) {
769 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400770 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400771 if ((*iter)->fConstantPropagation) {
772 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
773 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400774 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400775 if (!try_replace_expression(&b, iter, &optimized)) {
776 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400777 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400778 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400779 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400780 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400781 }
782 }
783 switch (expr->fKind) {
784 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400785 const VariableReference& ref = (VariableReference&) *expr;
786 const Variable& var = ref.fVariable;
787 if (ref.refKind() != VariableReference::kWrite_RefKind &&
788 ref.refKind() != VariableReference::kPointer_RefKind &&
789 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400790 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
791 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000792 this->error(expr->fOffset,
793 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400794 }
795 break;
796 }
797 case Expression::kTernary_Kind: {
798 TernaryExpression* t = (TernaryExpression*) expr;
799 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
800 // ternary has a constant test, replace it with either the true or
801 // false branch
802 if (((BoolLiteral&) *t->fTest).fValue) {
803 (*iter)->setExpression(std::move(t->fIfTrue));
804 } else {
805 (*iter)->setExpression(std::move(t->fIfFalse));
806 }
807 *outUpdated = true;
808 *outNeedsRescan = true;
809 }
810 break;
811 }
812 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400813 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400814 if (dead_assignment(*bin)) {
815 delete_left(&b, iter, outUpdated, outNeedsRescan);
816 break;
817 }
818 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400819 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
820 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
821 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
822 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
823 break;
824 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400825 switch (bin->fOperator) {
826 case Token::STAR:
827 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400828 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
829 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400830 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400831 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
832 } else {
833 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400834 // 1 * float4(x) -> float4(x)
835 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400836 delete_left(&b, iter, outUpdated, outNeedsRescan);
837 }
838 }
839 else if (is_constant(*bin->fLeft, 0)) {
840 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500841 bin->fRight->fType.kind() == Type::kVector_Kind &&
842 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400843 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400844 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
845 } else {
846 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400847 // float4(0) * x -> float4(0)
848 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500849 if (!bin->fRight->hasSideEffects()) {
850 delete_right(&b, iter, outUpdated, outNeedsRescan);
851 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400852 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400853 }
854 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400855 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
856 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400857 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400858 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
859 } else {
860 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400861 // float4(x) * 1 -> float4(x)
862 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400863 delete_right(&b, iter, outUpdated, outNeedsRescan);
864 }
865 }
866 else if (is_constant(*bin->fRight, 0)) {
867 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500868 bin->fRight->fType.kind() == Type::kScalar_Kind &&
869 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400870 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400871 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
872 } else {
873 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400874 // x * float4(0) -> float4(0)
875 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500876 if (!bin->fLeft->hasSideEffects()) {
877 delete_left(&b, iter, outUpdated, outNeedsRescan);
878 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400879 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400880 }
881 break;
Ethan Nicholas56e42712017-04-21 10:23:37 -0400882 case Token::PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400883 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400884 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
885 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400886 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400887 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
888 } else {
889 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400890 // 0 + float4(x) -> float4(x)
891 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400892 delete_left(&b, iter, outUpdated, outNeedsRescan);
893 }
894 } else if (is_constant(*bin->fRight, 0)) {
895 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
896 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400897 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400898 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
899 } else {
900 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400901 // float4(x) + 0 -> float4(x)
902 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400903 delete_right(&b, iter, outUpdated, outNeedsRescan);
904 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400905 }
906 break;
907 case Token::MINUS:
908 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400909 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 Nicholascb670962017-04-20 19:31:52 -0400919 }
920 break;
921 case Token::SLASH:
922 if (is_constant(*bin->fRight, 1)) {
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(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400926 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
927 } else {
928 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400929 // float4(x) / 1 -> float4(x)
930 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400931 delete_right(&b, iter, outUpdated, outNeedsRescan);
932 }
933 } else if (is_constant(*bin->fLeft, 0)) {
934 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500935 bin->fRight->fType.kind() == Type::kVector_Kind &&
936 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400938 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
939 } else {
940 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // float4(0) / x -> float4(0)
942 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500943 if (!bin->fRight->hasSideEffects()) {
944 delete_right(&b, iter, outUpdated, outNeedsRescan);
945 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400946 }
947 }
948 break;
949 case Token::PLUSEQ:
950 if (is_constant(*bin->fRight, 0)) {
951 clear_write(*bin->fLeft);
952 delete_right(&b, iter, outUpdated, outNeedsRescan);
953 }
954 break;
955 case Token::MINUSEQ:
956 if (is_constant(*bin->fRight, 0)) {
957 clear_write(*bin->fLeft);
958 delete_right(&b, iter, outUpdated, outNeedsRescan);
959 }
960 break;
961 case Token::STAREQ:
962 if (is_constant(*bin->fRight, 1)) {
963 clear_write(*bin->fLeft);
964 delete_right(&b, iter, outUpdated, outNeedsRescan);
965 }
966 break;
967 case Token::SLASHEQ:
968 if (is_constant(*bin->fRight, 1)) {
969 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400970 delete_right(&b, iter, outUpdated, outNeedsRescan);
971 }
972 break;
973 default:
974 break;
975 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -0400976 break;
977 }
978 case Expression::kSwizzle_Kind: {
979 Swizzle& s = (Swizzle&) *expr;
980 // detect identity swizzles like foo.rgba
981 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
982 bool identity = true;
983 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
984 if (s.fComponents[i] != i) {
985 identity = false;
986 break;
987 }
988 }
989 if (identity) {
990 *outUpdated = true;
991 if (!try_replace_expression(&b, iter, &s.fBase)) {
992 *outNeedsRescan = true;
993 return;
994 }
995 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
996 break;
997 }
998 }
999 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1000 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1001 Swizzle& base = (Swizzle&) *s.fBase;
1002 std::vector<int> final;
1003 for (int c : s.fComponents) {
1004 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1005 final.push_back(c);
1006 } else {
1007 final.push_back(base.fComponents[c]);
1008 }
1009 }
1010 *outUpdated = true;
1011 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1012 std::move(final)));
1013 if (!try_replace_expression(&b, iter, &replacement)) {
1014 *outNeedsRescan = true;
1015 return;
1016 }
1017 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1018 break;
1019 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001020 }
1021 default:
1022 break;
1023 }
1024}
1025
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001026// returns true if this statement could potentially execute a break at the current level (we ignore
1027// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
Ethan Nicholas5005a222018-08-24 13:06:27 -04001028static bool contains_conditional_break(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001029 switch (s.fKind) {
1030 case Statement::kBlock_Kind:
1031 for (const auto& sub : ((Block&) s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001032 if (contains_conditional_break(*sub, inConditional)) {
1033 return true;
1034 }
1035 }
1036 return false;
1037 case Statement::kBreak_Kind:
1038 return inConditional;
1039 case Statement::kIf_Kind: {
1040 const IfStatement& i = (IfStatement&) s;
1041 return contains_conditional_break(*i.fIfTrue, true) ||
1042 (i.fIfFalse && contains_conditional_break(*i.fIfFalse, true));
1043 }
1044 default:
1045 return false;
1046 }
1047}
1048
1049// returns true if this statement definitely executes a break at the current level (we ignore
1050// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1051static bool contains_unconditional_break(Statement& s) {
1052 switch (s.fKind) {
1053 case Statement::kBlock_Kind:
1054 for (const auto& sub : ((Block&) s).fStatements) {
1055 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001056 return true;
1057 }
1058 }
1059 return false;
1060 case Statement::kBreak_Kind:
1061 return true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001062 default:
1063 return false;
1064 }
1065}
1066
1067// Returns a block containing all of the statements that will be run if the given case matches
1068// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1069// broken by this call and must then be discarded).
1070// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1071// when break statements appear inside conditionals.
1072static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) {
1073 bool capturing = false;
1074 std::vector<std::unique_ptr<Statement>*> statementPtrs;
1075 for (const auto& current : s->fCases) {
1076 if (current.get() == c) {
1077 capturing = true;
1078 }
1079 if (capturing) {
1080 for (auto& stmt : current->fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001081 if (contains_conditional_break(*stmt, s->fKind == Statement::kIf_Kind)) {
1082 return nullptr;
1083 }
1084 if (contains_unconditional_break(*stmt)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001085 capturing = false;
1086 break;
1087 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001088 statementPtrs.push_back(&stmt);
1089 }
1090 if (!capturing) {
1091 break;
1092 }
1093 }
1094 }
1095 std::vector<std::unique_ptr<Statement>> statements;
1096 for (const auto& s : statementPtrs) {
1097 statements.push_back(std::move(*s));
1098 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001099 return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001100}
1101
Ethan Nicholascb670962017-04-20 19:31:52 -04001102void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001103 BasicBlock& b,
1104 std::vector<BasicBlock::Node>::iterator* iter,
1105 std::unordered_set<const Variable*>* undefinedVariables,
1106 bool* outUpdated,
1107 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001108 Statement* stmt = (*iter)->statement()->get();
1109 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001110 case Statement::kVarDeclaration_Kind: {
1111 const auto& varDecl = (VarDeclaration&) *stmt;
1112 if (varDecl.fVar->dead() &&
1113 (!varDecl.fValue ||
1114 !varDecl.fValue->hasSideEffects())) {
1115 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001116 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001117 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1118 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001119 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001120 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001121 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001122 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001123 }
1124 break;
1125 }
1126 case Statement::kIf_Kind: {
1127 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001128 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1129 // constant if, collapse down to a single branch
1130 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001131 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001132 (*iter)->setStatement(std::move(i.fIfTrue));
1133 } else {
1134 if (i.fIfFalse) {
1135 (*iter)->setStatement(std::move(i.fIfFalse));
1136 } else {
1137 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1138 }
1139 }
1140 *outUpdated = true;
1141 *outNeedsRescan = true;
1142 break;
1143 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001144 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1145 // else block doesn't do anything, remove it
1146 i.fIfFalse.reset();
1147 *outUpdated = true;
1148 *outNeedsRescan = true;
1149 }
1150 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1151 // if block doesn't do anything, no else block
1152 if (i.fTest->hasSideEffects()) {
1153 // test has side effects, keep it
1154 (*iter)->setStatement(std::unique_ptr<Statement>(
1155 new ExpressionStatement(std::move(i.fTest))));
1156 } else {
1157 // no if, no else, no test side effects, kill the whole if
1158 // statement
1159 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1160 }
1161 *outUpdated = true;
1162 *outNeedsRescan = true;
1163 }
1164 break;
1165 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001166 case Statement::kSwitch_Kind: {
1167 SwitchStatement& s = (SwitchStatement&) *stmt;
1168 if (s.fValue->isConstant()) {
1169 // switch is constant, replace it with the case that matches
1170 bool found = false;
1171 SwitchCase* defaultCase = nullptr;
1172 for (const auto& c : s.fCases) {
1173 if (!c->fValue) {
1174 defaultCase = c.get();
1175 continue;
1176 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001177 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001178 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001179 if (found) {
1180 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1181 if (newBlock) {
1182 (*iter)->setStatement(std::move(newBlock));
1183 break;
1184 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001185 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001186 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001187 "static switch contains non-static conditional break");
1188 s.fIsStatic = false;
1189 }
1190 return; // can't simplify
1191 }
1192 }
1193 }
1194 if (!found) {
1195 // no matching case. use default if it exists, or kill the whole thing
1196 if (defaultCase) {
1197 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1198 if (newBlock) {
1199 (*iter)->setStatement(std::move(newBlock));
1200 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001201 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001202 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001203 "static switch contains non-static conditional break");
1204 s.fIsStatic = false;
1205 }
1206 return; // can't simplify
1207 }
1208 } else {
1209 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1210 }
1211 }
1212 *outUpdated = true;
1213 *outNeedsRescan = true;
1214 }
1215 break;
1216 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001217 case Statement::kExpression_Kind: {
1218 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001219 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001220 if (!e.fExpression->hasSideEffects()) {
1221 // Expression statement with no side effects, kill it
1222 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1223 *outNeedsRescan = true;
1224 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001225 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001226 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1227 *outUpdated = true;
1228 }
1229 break;
1230 }
1231 default:
1232 break;
1233 }
1234}
1235
1236void Compiler::scanCFG(FunctionDefinition& f) {
1237 CFG cfg = CFGGenerator().getCFG(f);
1238 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001239
1240 // check for unreachable code
1241 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001242 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001243 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001244 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001245 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1246 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001247 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001248 break;
1249 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001250 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001251 break;
1252 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001253 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001254 }
1255 }
1256 if (fErrorCount) {
1257 return;
1258 }
1259
Ethan Nicholascb670962017-04-20 19:31:52 -04001260 // check for dead code & undefined variables, perform constant propagation
1261 std::unordered_set<const Variable*> undefinedVariables;
1262 bool updated;
1263 bool needsRescan = false;
1264 do {
1265 if (needsRescan) {
1266 cfg = CFGGenerator().getCFG(f);
1267 this->computeDataFlow(&cfg);
1268 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001269 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001270
1271 updated = false;
1272 for (BasicBlock& b : cfg.fBlocks) {
1273 DefinitionMap definitions = b.fBefore;
1274
1275 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1276 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1277 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1278 &needsRescan);
1279 } else {
1280 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1281 &needsRescan);
1282 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001283 if (needsRescan) {
1284 break;
1285 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001286 this->addDefinitions(*iter, &definitions);
1287 }
1288 }
1289 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001290 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001291
Ethan Nicholas91a10532017-06-22 11:24:38 -04001292 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001293 for (BasicBlock& b : cfg.fBlocks) {
1294 DefinitionMap definitions = b.fBefore;
1295
Ethan Nicholas91a10532017-06-22 11:24:38 -04001296 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001297 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1298 const Statement& s = **iter->statement();
1299 switch (s.fKind) {
1300 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001301 if (((const IfStatement&) s).fIsStatic &&
1302 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001303 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001304 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001305 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001306 break;
1307 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001308 if (((const SwitchStatement&) s).fIsStatic &&
1309 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001310 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001311 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001312 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001313 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001314 case Statement::kVarDeclarations_Kind: {
1315 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1316 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1317 if ((*varIter)->fKind == Statement::kNop_Kind) {
1318 varIter = decls.fVars.erase(varIter);
1319 } else {
1320 ++varIter;
1321 }
1322 }
1323 if (!decls.fVars.size()) {
1324 iter = b.fNodes.erase(iter);
1325 } else {
1326 ++iter;
1327 }
1328 break;
1329 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001330 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001331 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001332 break;
1333 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001334 } else {
1335 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001336 }
1337 }
1338 }
1339
ethannicholas22f939e2016-10-13 13:25:34 -07001340 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001341 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001342 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001343 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1344 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001345 }
1346 }
1347}
1348
Ethan Nicholas91164d12019-05-15 15:29:54 -04001349void Compiler::registerExternalValue(ExternalValue* value) {
1350 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1351}
1352
1353Symbol* Compiler::takeOwnership(std::unique_ptr<Symbol> symbol) {
1354 return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol));
1355}
1356
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001357std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001358 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001359 fErrorText = "";
1360 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001361 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001362 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001363 switch (kind) {
1364 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001365 inherited = &fVertexInclude;
1366 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001367 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001368 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001369 break;
1370 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001371 inherited = &fFragmentInclude;
1372 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001373 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001374 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001375 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001376 case Program::kGeometry_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001377 inherited = &fGeometryInclude;
1378 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001379 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001380 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001381 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001382 case Program::kFragmentProcessor_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001383 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001384 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001385 fIRGenerator->start(&settings, nullptr);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001386 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Robert Phillipsfe8da172018-01-24 14:52:02 +00001387 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes,
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001388 &elements);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001389 fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001390 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001391 case Program::kPipelineStage_Kind:
1392 inherited = &fPipelineInclude;
1393 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001394 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001395 fIRGenerator->start(&settings, inherited);
1396 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001397 case Program::kGeneric_Kind:
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001398 inherited = &fInterpreterInclude;
1399 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001400 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001401 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001402 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001403 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001404 for (auto& element : elements) {
1405 if (element->fKind == ProgramElement::kEnum_Kind) {
1406 ((Enum&) *element).fBuiltin = true;
1407 }
1408 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001409 std::unique_ptr<String> textPtr(new String(std::move(text)));
1410 fSource = textPtr.get();
Robert Phillipsfe8da172018-01-24 14:52:02 +00001411 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001412 auto result = std::unique_ptr<Program>(new Program(kind,
1413 std::move(textPtr),
1414 settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001415 fContext,
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001416 inherited,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001417 std::move(elements),
1418 fIRGenerator->fSymbolTable,
1419 fIRGenerator->fInputs));
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001420 if (fErrorCount) {
1421 return nullptr;
1422 }
1423 return result;
1424}
1425
Ethan Nicholas00543112018-07-31 09:44:36 -04001426bool Compiler::optimize(Program& program) {
1427 SkASSERT(!fErrorCount);
1428 if (!program.fIsOptimized) {
1429 program.fIsOptimized = true;
1430 fIRGenerator->fKind = program.fKind;
1431 fIRGenerator->fSettings = &program.fSettings;
1432 for (auto& element : program) {
1433 if (element.fKind == ProgramElement::kFunction_Kind) {
1434 this->scanCFG((FunctionDefinition&) element);
1435 }
1436 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001437 if (program.fKind != Program::kFragmentProcessor_Kind) {
1438 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1439 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1440 VarDeclarations& vars = (VarDeclarations&) **iter;
1441 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1442 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1443 if (var.dead()) {
1444 varIter = vars.fVars.erase(varIter);
1445 } else {
1446 ++varIter;
1447 }
1448 }
1449 if (vars.fVars.size() == 0) {
1450 iter = program.fElements.erase(iter);
1451 continue;
1452 }
1453 }
1454 ++iter;
1455 }
1456 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001457 }
1458 return fErrorCount == 0;
1459}
1460
1461std::unique_ptr<Program> Compiler::specialize(
1462 Program& program,
1463 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1464 std::vector<std::unique_ptr<ProgramElement>> elements;
1465 for (const auto& e : program) {
1466 elements.push_back(e.clone());
1467 }
1468 Program::Settings settings;
1469 settings.fCaps = program.fSettings.fCaps;
1470 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1471 settings.fArgs.insert(*iter);
1472 }
1473 std::unique_ptr<Program> result(new Program(program.fKind,
1474 nullptr,
1475 settings,
1476 program.fContext,
1477 program.fInheritedElements,
1478 std::move(elements),
1479 program.fSymbols,
1480 program.fInputs));
1481 return result;
1482}
1483
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001484#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1485
Ethan Nicholas00543112018-07-31 09:44:36 -04001486bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1487 if (!this->optimize(program)) {
1488 return false;
1489 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001490#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001491 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001492 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001493 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001494 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001495 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001496 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001497 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001498 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001499 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001500 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1501 SkDebugf("SPIR-V validation error: %s\n", m);
1502 };
1503 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001504 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001505 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001506 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001507 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001508 }
1509#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001510 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001511 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001512 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001513 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001514#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001515 return result;
1516}
1517
Ethan Nicholas00543112018-07-31 09:44:36 -04001518bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001519 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001520 bool result = this->toSPIRV(program, buffer);
1521 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001522 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001523 }
1524 return result;
1525}
1526
Ethan Nicholas00543112018-07-31 09:44:36 -04001527bool Compiler::toGLSL(Program& program, OutputStream& out) {
1528 if (!this->optimize(program)) {
1529 return false;
1530 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001531 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001532 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001533 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001534 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001535 return result;
1536}
1537
Ethan Nicholas00543112018-07-31 09:44:36 -04001538bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001539 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001540 bool result = this->toGLSL(program, buffer);
1541 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001542 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001543 }
1544 return result;
1545}
1546
Ethan Nicholas00543112018-07-31 09:44:36 -04001547bool Compiler::toMetal(Program& program, OutputStream& out) {
1548 if (!this->optimize(program)) {
1549 return false;
1550 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001551 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001552 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001553 return result;
1554}
1555
Ethan Nicholas00543112018-07-31 09:44:36 -04001556bool Compiler::toMetal(Program& program, String* out) {
1557 if (!this->optimize(program)) {
1558 return false;
1559 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001560 StringStream buffer;
1561 bool result = this->toMetal(program, buffer);
1562 if (result) {
1563 *out = buffer.str();
1564 }
1565 return result;
1566}
1567
Ethan Nicholas00543112018-07-31 09:44:36 -04001568bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1569 if (!this->optimize(program)) {
1570 return false;
1571 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001572 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001573 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001574 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001575 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001576 return result;
1577}
1578
Ethan Nicholas00543112018-07-31 09:44:36 -04001579bool Compiler::toH(Program& program, String name, OutputStream& out) {
1580 if (!this->optimize(program)) {
1581 return false;
1582 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001583 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001584 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001585 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001586 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001587 return result;
1588}
1589
Brian Osman2e29ab52019-09-20 12:19:11 -04001590#endif
1591
1592#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Ethan Nicholas00543112018-07-31 09:44:36 -04001593bool Compiler::toPipelineStage(const Program& program, String* out,
Brian Osman2e29ab52019-09-20 12:19:11 -04001594 std::vector<FormatArg>* outFormatArgs,
1595 std::vector<GLSLFunction>* outFunctions) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001596 SkASSERT(program.fIsOptimized);
1597 fSource = program.fSource.get();
1598 StringStream buffer;
Brian Osman2e29ab52019-09-20 12:19:11 -04001599 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outFormatArgs,
1600 outFunctions);
Ethan Nicholas00543112018-07-31 09:44:36 -04001601 bool result = cg.generateCode();
1602 fSource = nullptr;
1603 if (result) {
1604 *out = buffer.str();
1605 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001606 return result;
1607}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001608#endif
1609
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001610std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001611#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001612 if (!this->optimize(program)) {
1613 return nullptr;
1614 }
1615 std::unique_ptr<ByteCode> result(new ByteCode());
1616 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1617 if (cg.generateCode()) {
1618 return result;
1619 }
Brian Osman489cf882019-07-09 10:48:28 -04001620#else
1621 ABORT("ByteCode interpreter not enabled");
1622#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001623 return nullptr;
1624}
1625
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001626const char* Compiler::OperatorName(Token::Kind kind) {
1627 switch (kind) {
1628 case Token::PLUS: return "+";
1629 case Token::MINUS: return "-";
1630 case Token::STAR: return "*";
1631 case Token::SLASH: return "/";
1632 case Token::PERCENT: return "%";
1633 case Token::SHL: return "<<";
1634 case Token::SHR: return ">>";
1635 case Token::LOGICALNOT: return "!";
1636 case Token::LOGICALAND: return "&&";
1637 case Token::LOGICALOR: return "||";
1638 case Token::LOGICALXOR: return "^^";
1639 case Token::BITWISENOT: return "~";
1640 case Token::BITWISEAND: return "&";
1641 case Token::BITWISEOR: return "|";
1642 case Token::BITWISEXOR: return "^";
1643 case Token::EQ: return "=";
1644 case Token::EQEQ: return "==";
1645 case Token::NEQ: return "!=";
1646 case Token::LT: return "<";
1647 case Token::GT: return ">";
1648 case Token::LTEQ: return "<=";
1649 case Token::GTEQ: return ">=";
1650 case Token::PLUSEQ: return "+=";
1651 case Token::MINUSEQ: return "-=";
1652 case Token::STAREQ: return "*=";
1653 case Token::SLASHEQ: return "/=";
1654 case Token::PERCENTEQ: return "%=";
1655 case Token::SHLEQ: return "<<=";
1656 case Token::SHREQ: return ">>=";
1657 case Token::LOGICALANDEQ: return "&&=";
1658 case Token::LOGICALOREQ: return "||=";
1659 case Token::LOGICALXOREQ: return "^^=";
1660 case Token::BITWISEANDEQ: return "&=";
1661 case Token::BITWISEOREQ: return "|=";
1662 case Token::BITWISEXOREQ: return "^=";
1663 case Token::PLUSPLUS: return "++";
1664 case Token::MINUSMINUS: return "--";
1665 case Token::COMMA: return ",";
1666 default:
1667 ABORT("unsupported operator: %d\n", kind);
1668 }
1669}
1670
1671
1672bool Compiler::IsAssignment(Token::Kind op) {
1673 switch (op) {
1674 case Token::EQ: // fall through
1675 case Token::PLUSEQ: // fall through
1676 case Token::MINUSEQ: // fall through
1677 case Token::STAREQ: // fall through
1678 case Token::SLASHEQ: // fall through
1679 case Token::PERCENTEQ: // fall through
1680 case Token::SHLEQ: // fall through
1681 case Token::SHREQ: // fall through
1682 case Token::BITWISEOREQ: // fall through
1683 case Token::BITWISEXOREQ: // fall through
1684 case Token::BITWISEANDEQ: // fall through
1685 case Token::LOGICALOREQ: // fall through
1686 case Token::LOGICALXOREQ: // fall through
1687 case Token::LOGICALANDEQ:
1688 return true;
1689 default:
1690 return false;
1691 }
1692}
1693
1694Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001695 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001696 int line = 1;
1697 int column = 1;
1698 for (int i = 0; i < offset; i++) {
1699 if ((*fSource)[i] == '\n') {
1700 ++line;
1701 column = 1;
1702 }
1703 else {
1704 ++column;
1705 }
1706 }
1707 return Position(line, column);
1708}
1709
1710void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001711 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001712 Position pos = this->position(offset);
1713 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001714}
1715
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001716String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001717 this->writeErrorCount();
1718 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001719 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001720 return result;
1721}
1722
1723void Compiler::writeErrorCount() {
1724 if (fErrorCount) {
1725 fErrorText += to_string(fErrorCount) + " error";
1726 if (fErrorCount > 1) {
1727 fErrorText += "s";
1728 }
1729 fErrorText += "\n";
1730 }
1731}
1732
ethannicholasb3058bd2016-07-01 08:22:01 -07001733} // namespace