ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 30 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 31 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 32 | #include "spirv-tools/libspirv.hpp" |
| 33 | #endif |
| 34 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | // include the built-in shader symbols as static strings |
| 36 | |
Ethan Nicholas | 7970765 | 2017-11-16 11:20:11 -0500 | [diff] [blame] | 37 | #define STRINGIFY(x) #x |
| 38 | |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 39 | static const char* SKSL_GPU_INCLUDE = |
| 40 | #include "sksl_gpu.inc" |
| 41 | ; |
| 42 | |
| 43 | static const char* SKSL_INTERP_INCLUDE = |
| 44 | #include "sksl_interp.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 45 | ; |
| 46 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 47 | static const char* SKSL_VERT_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 48 | #include "sksl_vert.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | ; |
| 50 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 51 | static const char* SKSL_FRAG_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 52 | #include "sksl_frag.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | ; |
| 54 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 55 | static const char* SKSL_GEOM_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 56 | #include "sksl_geom.inc" |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 57 | ; |
| 58 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 59 | static const char* SKSL_FP_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 60 | #include "sksl_enums.inc" |
| 61 | #include "sksl_fp.inc" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 62 | ; |
| 63 | |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 64 | static const char* SKSL_PIPELINE_INCLUDE = |
| 65 | #include "sksl_pipeline.inc" |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 66 | ; |
| 67 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | namespace SkSL { |
| 69 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 70 | Compiler::Compiler(Flags flags) |
| 71 | : fFlags(flags) |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 72 | , fContext(new Context()) |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 73 | , fErrorCount(0) { |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 74 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this)); |
| 75 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this)); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 76 | fIRGenerator = new IRGenerator(fContext.get(), symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 77 | fTypes = types; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 78 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \ |
| 79 | fContext->f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 80 | ADD_TYPE(Void); |
| 81 | ADD_TYPE(Float); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 82 | ADD_TYPE(Float2); |
| 83 | ADD_TYPE(Float3); |
| 84 | ADD_TYPE(Float4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 85 | ADD_TYPE(Half); |
| 86 | ADD_TYPE(Half2); |
| 87 | ADD_TYPE(Half3); |
| 88 | ADD_TYPE(Half4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 89 | ADD_TYPE(Double); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 90 | ADD_TYPE(Double2); |
| 91 | ADD_TYPE(Double3); |
| 92 | ADD_TYPE(Double4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | ADD_TYPE(Int); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 94 | ADD_TYPE(Int2); |
| 95 | ADD_TYPE(Int3); |
| 96 | ADD_TYPE(Int4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 97 | ADD_TYPE(UInt); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 98 | ADD_TYPE(UInt2); |
| 99 | ADD_TYPE(UInt3); |
| 100 | ADD_TYPE(UInt4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 101 | ADD_TYPE(Short); |
| 102 | ADD_TYPE(Short2); |
| 103 | ADD_TYPE(Short3); |
| 104 | ADD_TYPE(Short4); |
| 105 | ADD_TYPE(UShort); |
| 106 | ADD_TYPE(UShort2); |
| 107 | ADD_TYPE(UShort3); |
| 108 | ADD_TYPE(UShort4); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 109 | ADD_TYPE(Byte); |
| 110 | ADD_TYPE(Byte2); |
| 111 | ADD_TYPE(Byte3); |
| 112 | ADD_TYPE(Byte4); |
| 113 | ADD_TYPE(UByte); |
| 114 | ADD_TYPE(UByte2); |
| 115 | ADD_TYPE(UByte3); |
| 116 | ADD_TYPE(UByte4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 117 | ADD_TYPE(Bool); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 118 | ADD_TYPE(Bool2); |
| 119 | ADD_TYPE(Bool3); |
| 120 | ADD_TYPE(Bool4); |
| 121 | ADD_TYPE(Float2x2); |
| 122 | ADD_TYPE(Float2x3); |
| 123 | ADD_TYPE(Float2x4); |
| 124 | ADD_TYPE(Float3x2); |
| 125 | ADD_TYPE(Float3x3); |
| 126 | ADD_TYPE(Float3x4); |
| 127 | ADD_TYPE(Float4x2); |
| 128 | ADD_TYPE(Float4x3); |
| 129 | ADD_TYPE(Float4x4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 130 | ADD_TYPE(Half2x2); |
| 131 | ADD_TYPE(Half2x3); |
| 132 | ADD_TYPE(Half2x4); |
| 133 | ADD_TYPE(Half3x2); |
| 134 | ADD_TYPE(Half3x3); |
| 135 | ADD_TYPE(Half3x4); |
| 136 | ADD_TYPE(Half4x2); |
| 137 | ADD_TYPE(Half4x3); |
| 138 | ADD_TYPE(Half4x4); |
| 139 | ADD_TYPE(Double2x2); |
| 140 | ADD_TYPE(Double2x3); |
| 141 | ADD_TYPE(Double2x4); |
| 142 | ADD_TYPE(Double3x2); |
| 143 | ADD_TYPE(Double3x3); |
| 144 | ADD_TYPE(Double3x4); |
| 145 | ADD_TYPE(Double4x2); |
| 146 | ADD_TYPE(Double4x3); |
| 147 | ADD_TYPE(Double4x4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 148 | ADD_TYPE(GenType); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 149 | ADD_TYPE(GenHType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 150 | ADD_TYPE(GenDType); |
| 151 | ADD_TYPE(GenIType); |
| 152 | ADD_TYPE(GenUType); |
| 153 | ADD_TYPE(GenBType); |
| 154 | ADD_TYPE(Mat); |
| 155 | ADD_TYPE(Vec); |
| 156 | ADD_TYPE(GVec); |
| 157 | ADD_TYPE(GVec2); |
| 158 | ADD_TYPE(GVec3); |
| 159 | ADD_TYPE(GVec4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 160 | ADD_TYPE(HVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | ADD_TYPE(DVec); |
| 162 | ADD_TYPE(IVec); |
| 163 | ADD_TYPE(UVec); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 164 | ADD_TYPE(SVec); |
| 165 | ADD_TYPE(USVec); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 166 | ADD_TYPE(ByteVec); |
| 167 | ADD_TYPE(UByteVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | ADD_TYPE(BVec); |
| 169 | |
| 170 | ADD_TYPE(Sampler1D); |
| 171 | ADD_TYPE(Sampler2D); |
| 172 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 173 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | ADD_TYPE(SamplerCube); |
| 175 | ADD_TYPE(Sampler2DRect); |
| 176 | ADD_TYPE(Sampler1DArray); |
| 177 | ADD_TYPE(Sampler2DArray); |
| 178 | ADD_TYPE(SamplerCubeArray); |
| 179 | ADD_TYPE(SamplerBuffer); |
| 180 | ADD_TYPE(Sampler2DMS); |
| 181 | ADD_TYPE(Sampler2DMSArray); |
| 182 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 183 | ADD_TYPE(ISampler2D); |
| 184 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 185 | ADD_TYPE(Image2D); |
| 186 | ADD_TYPE(IImage2D); |
| 187 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 188 | ADD_TYPE(SubpassInput); |
| 189 | ADD_TYPE(SubpassInputMS); |
| 190 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 191 | ADD_TYPE(GSampler1D); |
| 192 | ADD_TYPE(GSampler2D); |
| 193 | ADD_TYPE(GSampler3D); |
| 194 | ADD_TYPE(GSamplerCube); |
| 195 | ADD_TYPE(GSampler2DRect); |
| 196 | ADD_TYPE(GSampler1DArray); |
| 197 | ADD_TYPE(GSampler2DArray); |
| 198 | ADD_TYPE(GSamplerCubeArray); |
| 199 | ADD_TYPE(GSamplerBuffer); |
| 200 | ADD_TYPE(GSampler2DMS); |
| 201 | ADD_TYPE(GSampler2DMSArray); |
| 202 | |
| 203 | ADD_TYPE(Sampler1DShadow); |
| 204 | ADD_TYPE(Sampler2DShadow); |
| 205 | ADD_TYPE(SamplerCubeShadow); |
| 206 | ADD_TYPE(Sampler2DRectShadow); |
| 207 | ADD_TYPE(Sampler1DArrayShadow); |
| 208 | ADD_TYPE(Sampler2DArrayShadow); |
| 209 | ADD_TYPE(SamplerCubeArrayShadow); |
| 210 | ADD_TYPE(GSampler2DArrayShadow); |
| 211 | ADD_TYPE(GSamplerCubeArrayShadow); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 212 | ADD_TYPE(FragmentProcessor); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 213 | ADD_TYPE(SkRasterPipeline); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 214 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 215 | StringFragment skCapsName("sk_Caps"); |
| 216 | Variable* skCaps = new Variable(-1, Modifiers(), skCapsName, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 217 | *fContext->fSkCaps_Type, Variable::kGlobal_Storage); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 218 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 219 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 220 | StringFragment skArgsName("sk_Args"); |
| 221 | Variable* skArgs = new Variable(-1, Modifiers(), skArgsName, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 222 | *fContext->fSkArgs_Type, Variable::kGlobal_Storage); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 223 | fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs)); |
| 224 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 225 | std::vector<std::unique_ptr<ProgramElement>> ignored; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 226 | this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE), |
| 227 | symbols, &ignored, &fGpuSymbolTable); |
| 228 | this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE), |
| 229 | fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable); |
| 230 | this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE), |
| 231 | fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable); |
| 232 | this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, strlen(SKSL_GEOM_INCLUDE), |
| 233 | fGpuSymbolTable, &fGeometryInclude, &fGeometrySymbolTable); |
| 234 | this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE, |
| 235 | strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude, |
| 236 | &fPipelineSymbolTable); |
| 237 | this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE, |
| 238 | strlen(SKSL_INTERP_INCLUDE), symbols, &fInterpreterInclude, |
| 239 | &fInterpreterSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | Compiler::~Compiler() { |
| 243 | delete fIRGenerator; |
| 244 | } |
| 245 | |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 246 | void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length, |
| 247 | std::shared_ptr<SymbolTable> base, |
| 248 | std::vector<std::unique_ptr<ProgramElement>>* outElements, |
| 249 | std::shared_ptr<SymbolTable>* outSymbolTable) { |
| 250 | fIRGenerator->fSymbolTable = std::move(base); |
| 251 | Program::Settings settings; |
| 252 | fIRGenerator->start(&settings, nullptr); |
| 253 | fIRGenerator->convertProgram(kind, src, length, *fTypes, outElements); |
| 254 | if (this->fErrorCount) { |
| 255 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
| 256 | } |
| 257 | SkASSERT(!fErrorCount); |
| 258 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
| 259 | *outSymbolTable = fIRGenerator->fSymbolTable; |
| 260 | } |
| 261 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 262 | // add the definition created by assigning to the lvalue to the definition set |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 263 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 264 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 265 | switch (lvalue->fKind) { |
| 266 | case Expression::kVariableReference_Kind: { |
| 267 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 268 | if (var.fStorage == Variable::kLocal_Storage) { |
| 269 | (*definitions)[&var] = expr; |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | case Expression::kSwizzle_Kind: |
| 274 | // We consider the variable written to as long as at least some of its components have |
| 275 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 276 | // write to foo.x and then read foo.y), but being stricter could lead to false positives |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 277 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 278 | // but since we pass foo as a whole it is flagged as an error) unless we perform a much |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 279 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 280 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 281 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 282 | definitions); |
| 283 | break; |
| 284 | case Expression::kIndex_Kind: |
| 285 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 286 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 287 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 288 | definitions); |
| 289 | break; |
| 290 | case Expression::kFieldAccess_Kind: |
| 291 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 292 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 293 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 294 | definitions); |
| 295 | break; |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 296 | case Expression::kTernary_Kind: |
| 297 | // To simplify analysis, we just pretend that we write to both sides of the ternary. |
| 298 | // This allows for false positives (meaning we fail to detect that a variable might not |
| 299 | // have been assigned), but is preferable to false negatives. |
| 300 | this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 301 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 302 | definitions); |
| 303 | this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 304 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 305 | definitions); |
| 306 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 307 | case Expression::kExternalValue_Kind: |
| 308 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | default: |
| 310 | // not an lvalue, can't happen |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 311 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 316 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 317 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 318 | switch (node.fKind) { |
| 319 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 320 | SkASSERT(node.expression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 321 | const Expression* expr = (Expression*) node.expression()->get(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 322 | switch (expr->fKind) { |
| 323 | case Expression::kBinary_Kind: { |
| 324 | BinaryExpression* b = (BinaryExpression*) expr; |
| 325 | if (b->fOperator == Token::EQ) { |
| 326 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 327 | } else if (Compiler::IsAssignment(b->fOperator)) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 328 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 329 | b->fLeft.get(), |
| 330 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 331 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 332 | |
| 333 | } |
| 334 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 335 | } |
Ethan Nicholas | c6a19f1 | 2018-03-29 16:46:56 -0400 | [diff] [blame] | 336 | case Expression::kFunctionCall_Kind: { |
| 337 | const FunctionCall& c = (const FunctionCall&) *expr; |
| 338 | for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) { |
| 339 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 340 | this->addDefinition( |
| 341 | c.fArguments[i].get(), |
| 342 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 343 | definitions); |
| 344 | } |
| 345 | } |
| 346 | break; |
| 347 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 348 | case Expression::kPrefix_Kind: { |
| 349 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 350 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 351 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 352 | p->fOperand.get(), |
| 353 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 354 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 355 | } |
| 356 | break; |
| 357 | } |
| 358 | case Expression::kPostfix_Kind: { |
| 359 | const PostfixExpression* p = (PostfixExpression*) expr; |
| 360 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 361 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 362 | p->fOperand.get(), |
| 363 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 364 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 365 | } |
| 366 | break; |
| 367 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 368 | case Expression::kVariableReference_Kind: { |
| 369 | const VariableReference* v = (VariableReference*) expr; |
| 370 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 371 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 372 | v, |
| 373 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 374 | definitions); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 377 | default: |
| 378 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 379 | } |
| 380 | break; |
| 381 | } |
| 382 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 383 | const Statement* stmt = (Statement*) node.statement()->get(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 384 | if (stmt->fKind == Statement::kVarDeclaration_Kind) { |
| 385 | VarDeclaration& vd = (VarDeclaration&) *stmt; |
| 386 | if (vd.fValue) { |
| 387 | (*definitions)[vd.fVar] = &vd.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 396 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 397 | |
| 398 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 399 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 400 | for (const BasicBlock::Node& n : block.fNodes) { |
| 401 | this->addDefinitions(n, &after); |
| 402 | } |
| 403 | |
| 404 | // propagate definitions to exits |
| 405 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 406 | if (exitId == blockId) { |
| 407 | continue; |
| 408 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 409 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 410 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 411 | std::unique_ptr<Expression>* e1 = pair.second; |
| 412 | auto found = exit.fBefore.find(pair.first); |
| 413 | if (found == exit.fBefore.end()) { |
| 414 | // exit has no definition for it, just copy it |
| 415 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 416 | exit.fBefore[pair.first] = e1; |
| 417 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 418 | // exit has a (possibly different) value already defined |
| 419 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 420 | if (e1 != e2) { |
| 421 | // definition has changed, merge and add exit block to worklist |
| 422 | workList->insert(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 423 | if (e1 && e2) { |
| 424 | exit.fBefore[pair.first] = |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 425 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 426 | } else { |
| 427 | exit.fBefore[pair.first] = nullptr; |
| 428 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 436 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 437 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 438 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 439 | for (const auto& block : cfg.fBlocks) { |
| 440 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 441 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 442 | SkASSERT(node.statement()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 443 | const Statement* s = node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 444 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 445 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 446 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 447 | if (decl->fKind == Statement::kVarDeclaration_Kind) { |
| 448 | result[((VarDeclaration&) *decl).fVar] = nullptr; |
| 449 | } |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 450 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return result; |
| 456 | } |
| 457 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 458 | /** |
| 459 | * Returns true if assigning to this lvalue has no effect. |
| 460 | */ |
| 461 | static bool is_dead(const Expression& lvalue) { |
| 462 | switch (lvalue.fKind) { |
| 463 | case Expression::kVariableReference_Kind: |
| 464 | return ((VariableReference&) lvalue).fVariable.dead(); |
| 465 | case Expression::kSwizzle_Kind: |
| 466 | return is_dead(*((Swizzle&) lvalue).fBase); |
| 467 | case Expression::kFieldAccess_Kind: |
| 468 | return is_dead(*((FieldAccess&) lvalue).fBase); |
| 469 | case Expression::kIndex_Kind: { |
| 470 | const IndexExpression& idx = (IndexExpression&) lvalue; |
| 471 | return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects(); |
| 472 | } |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 473 | case Expression::kTernary_Kind: { |
| 474 | const TernaryExpression& t = (TernaryExpression&) lvalue; |
| 475 | return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse); |
| 476 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 477 | case Expression::kExternalValue_Kind: |
| 478 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 479 | default: |
| 480 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
| 481 | } |
| 482 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 483 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 484 | /** |
| 485 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 486 | * to a dead target and lack of side effects on the left hand side. |
| 487 | */ |
| 488 | static bool dead_assignment(const BinaryExpression& b) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 489 | if (!Compiler::IsAssignment(b.fOperator)) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | return is_dead(*b.fLeft); |
| 493 | } |
| 494 | |
| 495 | void Compiler::computeDataFlow(CFG* cfg) { |
| 496 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 497 | std::set<BlockId> workList; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 498 | for (BlockId i = 0; i < cfg->fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 499 | workList.insert(i); |
| 500 | } |
| 501 | while (workList.size()) { |
| 502 | BlockId next = *workList.begin(); |
| 503 | workList.erase(workList.begin()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 504 | this->scanCFG(cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 505 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 510 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 511 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 512 | * need to be regenerated). |
| 513 | */ |
| 514 | bool try_replace_expression(BasicBlock* b, |
| 515 | std::vector<BasicBlock::Node>::iterator* iter, |
| 516 | std::unique_ptr<Expression>* newExpression) { |
| 517 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 518 | if (!b->tryRemoveExpression(iter)) { |
| 519 | *target = std::move(*newExpression); |
| 520 | return false; |
| 521 | } |
| 522 | *target = std::move(*newExpression); |
| 523 | return b->tryInsertExpression(iter, target); |
| 524 | } |
| 525 | |
| 526 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 527 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 528 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 529 | */ |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 530 | bool is_constant(const Expression& expr, double value) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 531 | switch (expr.fKind) { |
| 532 | case Expression::kIntLiteral_Kind: |
| 533 | return ((IntLiteral&) expr).fValue == value; |
| 534 | case Expression::kFloatLiteral_Kind: |
| 535 | return ((FloatLiteral&) expr).fValue == value; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 536 | case Expression::kConstructor_Kind: { |
| 537 | Constructor& c = (Constructor&) expr; |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 538 | bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat() |
| 539 | : c.fType.isFloat(); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 540 | if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) { |
| 541 | for (int i = 0; i < c.fType.columns(); ++i) { |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 542 | if (isFloat) { |
| 543 | if (c.getFVecComponent(i) != value) { |
| 544 | return false; |
| 545 | } |
| 546 | } else if (c.getIVecComponent(i) != value) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | return true; |
| 551 | } |
| 552 | return false; |
| 553 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 554 | default: |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 561 | * and CFG structures). |
| 562 | */ |
| 563 | void delete_left(BasicBlock* b, |
| 564 | std::vector<BasicBlock::Node>::iterator* iter, |
| 565 | bool* outUpdated, |
| 566 | bool* outNeedsRescan) { |
| 567 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 568 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 569 | SkASSERT((*target)->fKind == Expression::kBinary_Kind); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 570 | BinaryExpression& bin = (BinaryExpression&) **target; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 571 | SkASSERT(!bin.fLeft->hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 572 | bool result; |
| 573 | if (bin.fOperator == Token::EQ) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 574 | result = b->tryRemoveLValueBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 575 | } else { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 576 | result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 577 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 578 | *target = std::move(bin.fRight); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 579 | if (!result) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 580 | *outNeedsRescan = true; |
| 581 | return; |
| 582 | } |
| 583 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 584 | *outNeedsRescan = true; |
| 585 | return; |
| 586 | } |
| 587 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 588 | if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 589 | (*iter)->expression() != &bin.fRight) { |
| 590 | *outNeedsRescan = true; |
| 591 | return; |
| 592 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 593 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 594 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 599 | * CFG structures). |
| 600 | */ |
| 601 | void delete_right(BasicBlock* b, |
| 602 | std::vector<BasicBlock::Node>::iterator* iter, |
| 603 | bool* outUpdated, |
| 604 | bool* outNeedsRescan) { |
| 605 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 606 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 607 | SkASSERT((*target)->fKind == Expression::kBinary_Kind); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 608 | BinaryExpression& bin = (BinaryExpression&) **target; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 609 | SkASSERT(!bin.fRight->hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 610 | if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) { |
| 611 | *target = std::move(bin.fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 612 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 613 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 614 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 615 | *target = std::move(bin.fLeft); |
| 616 | if (*iter == b->fNodes.begin()) { |
| 617 | *outNeedsRescan = true; |
| 618 | return; |
| 619 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 620 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 621 | if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 622 | (*iter)->expression() != &bin.fLeft)) { |
| 623 | *outNeedsRescan = true; |
| 624 | return; |
| 625 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 626 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 627 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 628 | } |
| 629 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 630 | /** |
| 631 | * Constructs the specified type using a single argument. |
| 632 | */ |
| 633 | static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) { |
| 634 | std::vector<std::unique_ptr<Expression>> args; |
| 635 | args.push_back(std::move(v)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 636 | auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args))); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 637 | return result; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 642 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 643 | */ |
| 644 | static void vectorize(BasicBlock* b, |
| 645 | std::vector<BasicBlock::Node>::iterator* iter, |
| 646 | const Type& type, |
| 647 | std::unique_ptr<Expression>* otherExpression, |
| 648 | bool* outUpdated, |
| 649 | bool* outNeedsRescan) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 650 | SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind); |
| 651 | SkASSERT(type.kind() == Type::kVector_Kind); |
| 652 | SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 653 | *outUpdated = true; |
| 654 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 655 | if (!b->tryRemoveExpression(iter)) { |
| 656 | *target = construct(type, std::move(*otherExpression)); |
| 657 | *outNeedsRescan = true; |
| 658 | } else { |
| 659 | *target = construct(type, std::move(*otherExpression)); |
| 660 | if (!b->tryInsertExpression(iter, target)) { |
| 661 | *outNeedsRescan = true; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 668 | * left to yield vec<n>(x). |
| 669 | */ |
| 670 | static void vectorize_left(BasicBlock* b, |
| 671 | std::vector<BasicBlock::Node>::iterator* iter, |
| 672 | bool* outUpdated, |
| 673 | bool* outNeedsRescan) { |
| 674 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 675 | vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 680 | * right to yield vec<n>(y). |
| 681 | */ |
| 682 | static void vectorize_right(BasicBlock* b, |
| 683 | std::vector<BasicBlock::Node>::iterator* iter, |
| 684 | bool* outUpdated, |
| 685 | bool* outNeedsRescan) { |
| 686 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 687 | vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan); |
| 688 | } |
| 689 | |
| 690 | // Mark that an expression which we were writing to is no longer being written to |
| 691 | void clear_write(const Expression& expr) { |
| 692 | switch (expr.fKind) { |
| 693 | case Expression::kVariableReference_Kind: { |
| 694 | ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind); |
| 695 | break; |
| 696 | } |
| 697 | case Expression::kFieldAccess_Kind: |
| 698 | clear_write(*((FieldAccess&) expr).fBase); |
| 699 | break; |
| 700 | case Expression::kSwizzle_Kind: |
| 701 | clear_write(*((Swizzle&) expr).fBase); |
| 702 | break; |
| 703 | case Expression::kIndex_Kind: |
| 704 | clear_write(*((IndexExpression&) expr).fBase); |
| 705 | break; |
| 706 | default: |
| 707 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 712 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 713 | BasicBlock& b, |
| 714 | std::vector<BasicBlock::Node>::iterator* iter, |
| 715 | std::unordered_set<const Variable*>* undefinedVariables, |
| 716 | bool* outUpdated, |
| 717 | bool* outNeedsRescan) { |
| 718 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 719 | SkASSERT(expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 720 | if ((*iter)->fConstantPropagation) { |
| 721 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 722 | if (optimized) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 723 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 724 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 725 | *outNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 726 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 727 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 728 | SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 729 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | switch (expr->fKind) { |
| 733 | case Expression::kVariableReference_Kind: { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 734 | const VariableReference& ref = (VariableReference&) *expr; |
| 735 | const Variable& var = ref.fVariable; |
| 736 | if (ref.refKind() != VariableReference::kWrite_RefKind && |
| 737 | ref.refKind() != VariableReference::kPointer_RefKind && |
| 738 | var.fStorage == Variable::kLocal_Storage && !definitions[&var] && |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 739 | (*undefinedVariables).find(&var) == (*undefinedVariables).end()) { |
| 740 | (*undefinedVariables).insert(&var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 741 | this->error(expr->fOffset, |
| 742 | "'" + var.fName + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 743 | } |
| 744 | break; |
| 745 | } |
| 746 | case Expression::kTernary_Kind: { |
| 747 | TernaryExpression* t = (TernaryExpression*) expr; |
| 748 | if (t->fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 749 | // ternary has a constant test, replace it with either the true or |
| 750 | // false branch |
| 751 | if (((BoolLiteral&) *t->fTest).fValue) { |
| 752 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 753 | } else { |
| 754 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 755 | } |
| 756 | *outUpdated = true; |
| 757 | *outNeedsRescan = true; |
| 758 | } |
| 759 | break; |
| 760 | } |
| 761 | case Expression::kBinary_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 762 | BinaryExpression* bin = (BinaryExpression*) expr; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 763 | if (dead_assignment(*bin)) { |
| 764 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 765 | break; |
| 766 | } |
| 767 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 768 | if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) && |
| 769 | (bin->fLeft->fType.kind() != Type::kVector_Kind)) || |
| 770 | ((bin->fRight->fType.kind() != Type::kScalar_Kind) && |
| 771 | (bin->fRight->fType.kind() != Type::kVector_Kind))) { |
| 772 | break; |
| 773 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 774 | switch (bin->fOperator) { |
| 775 | case Token::STAR: |
| 776 | if (is_constant(*bin->fLeft, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 777 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 778 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 779 | // float4(1) * x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 780 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 781 | } else { |
| 782 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 783 | // 1 * float4(x) -> float4(x) |
| 784 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 785 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 786 | } |
| 787 | } |
| 788 | else if (is_constant(*bin->fLeft, 0)) { |
| 789 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 790 | bin->fRight->fType.kind() == Type::kVector_Kind && |
| 791 | !bin->fRight->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 792 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 793 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 794 | } else { |
| 795 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 796 | // float4(0) * x -> float4(0) |
| 797 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 798 | if (!bin->fRight->hasSideEffects()) { |
| 799 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 800 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 801 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 802 | } |
| 803 | else if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 804 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 805 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 806 | // x * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 807 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 808 | } else { |
| 809 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 810 | // float4(x) * 1 -> float4(x) |
| 811 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 812 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 813 | } |
| 814 | } |
| 815 | else if (is_constant(*bin->fRight, 0)) { |
| 816 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 817 | bin->fRight->fType.kind() == Type::kScalar_Kind && |
| 818 | !bin->fLeft->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 819 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 820 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 821 | } else { |
| 822 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 823 | // x * float4(0) -> float4(0) |
| 824 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 825 | if (!bin->fLeft->hasSideEffects()) { |
| 826 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 827 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 828 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 829 | } |
| 830 | break; |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 831 | case Token::PLUS: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 832 | if (is_constant(*bin->fLeft, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 833 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 834 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 835 | // float4(0) + x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 836 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 837 | } else { |
| 838 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 839 | // 0 + float4(x) -> float4(x) |
| 840 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 841 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 842 | } |
| 843 | } else if (is_constant(*bin->fRight, 0)) { |
| 844 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 845 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 846 | // x + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 847 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 848 | } else { |
| 849 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 850 | // float4(x) + 0 -> float4(x) |
| 851 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 852 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 853 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 854 | } |
| 855 | break; |
| 856 | case Token::MINUS: |
| 857 | if (is_constant(*bin->fRight, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 858 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 859 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 860 | // x - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 861 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 862 | } else { |
| 863 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 864 | // float4(x) - 0 -> float4(x) |
| 865 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 866 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 867 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 868 | } |
| 869 | break; |
| 870 | case Token::SLASH: |
| 871 | if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 872 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 873 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 874 | // x / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 875 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 876 | } else { |
| 877 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 878 | // float4(x) / 1 -> float4(x) |
| 879 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 880 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 881 | } |
| 882 | } else if (is_constant(*bin->fLeft, 0)) { |
| 883 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 884 | bin->fRight->fType.kind() == Type::kVector_Kind && |
| 885 | !bin->fRight->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 886 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 887 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 888 | } else { |
| 889 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 890 | // float4(0) / x -> float4(0) |
| 891 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 892 | if (!bin->fRight->hasSideEffects()) { |
| 893 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 894 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | break; |
| 898 | case Token::PLUSEQ: |
| 899 | if (is_constant(*bin->fRight, 0)) { |
| 900 | clear_write(*bin->fLeft); |
| 901 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 902 | } |
| 903 | break; |
| 904 | case Token::MINUSEQ: |
| 905 | if (is_constant(*bin->fRight, 0)) { |
| 906 | clear_write(*bin->fLeft); |
| 907 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 908 | } |
| 909 | break; |
| 910 | case Token::STAREQ: |
| 911 | if (is_constant(*bin->fRight, 1)) { |
| 912 | clear_write(*bin->fLeft); |
| 913 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 914 | } |
| 915 | break; |
| 916 | case Token::SLASHEQ: |
| 917 | if (is_constant(*bin->fRight, 1)) { |
| 918 | clear_write(*bin->fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 919 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 920 | } |
| 921 | break; |
| 922 | default: |
| 923 | break; |
| 924 | } |
| 925 | } |
| 926 | default: |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 931 | // returns true if this statement could potentially execute a break at the current level (we ignore |
| 932 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
Ethan Nicholas | 5005a22 | 2018-08-24 13:06:27 -0400 | [diff] [blame] | 933 | static bool contains_conditional_break(Statement& s, bool inConditional) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 934 | switch (s.fKind) { |
| 935 | case Statement::kBlock_Kind: |
| 936 | for (const auto& sub : ((Block&) s).fStatements) { |
Ethan Nicholas | 5005a22 | 2018-08-24 13:06:27 -0400 | [diff] [blame] | 937 | if (contains_conditional_break(*sub, inConditional)) { |
| 938 | return true; |
| 939 | } |
| 940 | } |
| 941 | return false; |
| 942 | case Statement::kBreak_Kind: |
| 943 | return inConditional; |
| 944 | case Statement::kIf_Kind: { |
| 945 | const IfStatement& i = (IfStatement&) s; |
| 946 | return contains_conditional_break(*i.fIfTrue, true) || |
| 947 | (i.fIfFalse && contains_conditional_break(*i.fIfFalse, true)); |
| 948 | } |
| 949 | default: |
| 950 | return false; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | // returns true if this statement definitely executes a break at the current level (we ignore |
| 955 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
| 956 | static bool contains_unconditional_break(Statement& s) { |
| 957 | switch (s.fKind) { |
| 958 | case Statement::kBlock_Kind: |
| 959 | for (const auto& sub : ((Block&) s).fStatements) { |
| 960 | if (contains_unconditional_break(*sub)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 961 | return true; |
| 962 | } |
| 963 | } |
| 964 | return false; |
| 965 | case Statement::kBreak_Kind: |
| 966 | return true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 967 | default: |
| 968 | return false; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Returns a block containing all of the statements that will be run if the given case matches |
| 973 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 974 | // broken by this call and must then be discarded). |
| 975 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 976 | // when break statements appear inside conditionals. |
| 977 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) { |
| 978 | bool capturing = false; |
| 979 | std::vector<std::unique_ptr<Statement>*> statementPtrs; |
| 980 | for (const auto& current : s->fCases) { |
| 981 | if (current.get() == c) { |
| 982 | capturing = true; |
| 983 | } |
| 984 | if (capturing) { |
| 985 | for (auto& stmt : current->fStatements) { |
Ethan Nicholas | 5005a22 | 2018-08-24 13:06:27 -0400 | [diff] [blame] | 986 | if (contains_conditional_break(*stmt, s->fKind == Statement::kIf_Kind)) { |
| 987 | return nullptr; |
| 988 | } |
| 989 | if (contains_unconditional_break(*stmt)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 990 | capturing = false; |
| 991 | break; |
| 992 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 993 | statementPtrs.push_back(&stmt); |
| 994 | } |
| 995 | if (!capturing) { |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | std::vector<std::unique_ptr<Statement>> statements; |
| 1001 | for (const auto& s : statementPtrs) { |
| 1002 | statements.push_back(std::move(*s)); |
| 1003 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1004 | return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols)); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1007 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1008 | BasicBlock& b, |
| 1009 | std::vector<BasicBlock::Node>::iterator* iter, |
| 1010 | std::unordered_set<const Variable*>* undefinedVariables, |
| 1011 | bool* outUpdated, |
| 1012 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1013 | Statement* stmt = (*iter)->statement()->get(); |
| 1014 | switch (stmt->fKind) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1015 | case Statement::kVarDeclaration_Kind: { |
| 1016 | const auto& varDecl = (VarDeclaration&) *stmt; |
| 1017 | if (varDecl.fVar->dead() && |
| 1018 | (!varDecl.fValue || |
| 1019 | !varDecl.fValue->hasSideEffects())) { |
| 1020 | if (varDecl.fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1021 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1022 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 1023 | *outNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1024 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1025 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1026 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1027 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1028 | } |
| 1029 | break; |
| 1030 | } |
| 1031 | case Statement::kIf_Kind: { |
| 1032 | IfStatement& i = (IfStatement&) *stmt; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1033 | if (i.fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 1034 | // constant if, collapse down to a single branch |
| 1035 | if (((BoolLiteral&) *i.fTest).fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1036 | SkASSERT(i.fIfTrue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1037 | (*iter)->setStatement(std::move(i.fIfTrue)); |
| 1038 | } else { |
| 1039 | if (i.fIfFalse) { |
| 1040 | (*iter)->setStatement(std::move(i.fIfFalse)); |
| 1041 | } else { |
| 1042 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1043 | } |
| 1044 | } |
| 1045 | *outUpdated = true; |
| 1046 | *outNeedsRescan = true; |
| 1047 | break; |
| 1048 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1049 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 1050 | // else block doesn't do anything, remove it |
| 1051 | i.fIfFalse.reset(); |
| 1052 | *outUpdated = true; |
| 1053 | *outNeedsRescan = true; |
| 1054 | } |
| 1055 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 1056 | // if block doesn't do anything, no else block |
| 1057 | if (i.fTest->hasSideEffects()) { |
| 1058 | // test has side effects, keep it |
| 1059 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 1060 | new ExpressionStatement(std::move(i.fTest)))); |
| 1061 | } else { |
| 1062 | // no if, no else, no test side effects, kill the whole if |
| 1063 | // statement |
| 1064 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1065 | } |
| 1066 | *outUpdated = true; |
| 1067 | *outNeedsRescan = true; |
| 1068 | } |
| 1069 | break; |
| 1070 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1071 | case Statement::kSwitch_Kind: { |
| 1072 | SwitchStatement& s = (SwitchStatement&) *stmt; |
| 1073 | if (s.fValue->isConstant()) { |
| 1074 | // switch is constant, replace it with the case that matches |
| 1075 | bool found = false; |
| 1076 | SwitchCase* defaultCase = nullptr; |
| 1077 | for (const auto& c : s.fCases) { |
| 1078 | if (!c->fValue) { |
| 1079 | defaultCase = c.get(); |
| 1080 | continue; |
| 1081 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1082 | SkASSERT(c->fValue->fKind == s.fValue->fKind); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1083 | found = c->fValue->compareConstant(*fContext, *s.fValue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1084 | if (found) { |
| 1085 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
| 1086 | if (newBlock) { |
| 1087 | (*iter)->setStatement(std::move(newBlock)); |
| 1088 | break; |
| 1089 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1090 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1091 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1092 | "static switch contains non-static conditional break"); |
| 1093 | s.fIsStatic = false; |
| 1094 | } |
| 1095 | return; // can't simplify |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | if (!found) { |
| 1100 | // no matching case. use default if it exists, or kill the whole thing |
| 1101 | if (defaultCase) { |
| 1102 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 1103 | if (newBlock) { |
| 1104 | (*iter)->setStatement(std::move(newBlock)); |
| 1105 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1106 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1107 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1108 | "static switch contains non-static conditional break"); |
| 1109 | s.fIsStatic = false; |
| 1110 | } |
| 1111 | return; // can't simplify |
| 1112 | } |
| 1113 | } else { |
| 1114 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1115 | } |
| 1116 | } |
| 1117 | *outUpdated = true; |
| 1118 | *outNeedsRescan = true; |
| 1119 | } |
| 1120 | break; |
| 1121 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1122 | case Statement::kExpression_Kind: { |
| 1123 | ExpressionStatement& e = (ExpressionStatement&) *stmt; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1124 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1125 | if (!e.fExpression->hasSideEffects()) { |
| 1126 | // Expression statement with no side effects, kill it |
| 1127 | if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) { |
| 1128 | *outNeedsRescan = true; |
| 1129 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1130 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1131 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1132 | *outUpdated = true; |
| 1133 | } |
| 1134 | break; |
| 1135 | } |
| 1136 | default: |
| 1137 | break; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | void Compiler::scanCFG(FunctionDefinition& f) { |
| 1142 | CFG cfg = CFGGenerator().getCFG(f); |
| 1143 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1144 | |
| 1145 | // check for unreachable code |
| 1146 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 1147 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1148 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1149 | int offset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1150 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 1151 | case BasicBlock::Node::kStatement_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1152 | offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1153 | break; |
| 1154 | case BasicBlock::Node::kExpression_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1155 | offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1156 | break; |
| 1157 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1158 | this->error(offset, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | if (fErrorCount) { |
| 1162 | return; |
| 1163 | } |
| 1164 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1165 | // check for dead code & undefined variables, perform constant propagation |
| 1166 | std::unordered_set<const Variable*> undefinedVariables; |
| 1167 | bool updated; |
| 1168 | bool needsRescan = false; |
| 1169 | do { |
| 1170 | if (needsRescan) { |
| 1171 | cfg = CFGGenerator().getCFG(f); |
| 1172 | this->computeDataFlow(&cfg); |
| 1173 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1174 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1175 | |
| 1176 | updated = false; |
| 1177 | for (BasicBlock& b : cfg.fBlocks) { |
| 1178 | DefinitionMap definitions = b.fBefore; |
| 1179 | |
| 1180 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
| 1181 | if (iter->fKind == BasicBlock::Node::kExpression_Kind) { |
| 1182 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 1183 | &needsRescan); |
| 1184 | } else { |
| 1185 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
| 1186 | &needsRescan); |
| 1187 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1188 | if (needsRescan) { |
| 1189 | break; |
| 1190 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1191 | this->addDefinitions(*iter, &definitions); |
| 1192 | } |
| 1193 | } |
| 1194 | } while (updated); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1195 | SkASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1196 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1197 | // verify static ifs & switches, clean up dead variable decls |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1198 | for (BasicBlock& b : cfg.fBlocks) { |
| 1199 | DefinitionMap definitions = b.fBefore; |
| 1200 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1201 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1202 | if (iter->fKind == BasicBlock::Node::kStatement_Kind) { |
| 1203 | const Statement& s = **iter->statement(); |
| 1204 | switch (s.fKind) { |
| 1205 | case Statement::kIf_Kind: |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1206 | if (((const IfStatement&) s).fIsStatic && |
| 1207 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1208 | this->error(s.fOffset, "static if has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1209 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1210 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1211 | break; |
| 1212 | case Statement::kSwitch_Kind: |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1213 | if (((const SwitchStatement&) s).fIsStatic && |
| 1214 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1215 | this->error(s.fOffset, "static switch has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1216 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1217 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1218 | break; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1219 | case Statement::kVarDeclarations_Kind: { |
| 1220 | VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration; |
| 1221 | for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) { |
| 1222 | if ((*varIter)->fKind == Statement::kNop_Kind) { |
| 1223 | varIter = decls.fVars.erase(varIter); |
| 1224 | } else { |
| 1225 | ++varIter; |
| 1226 | } |
| 1227 | } |
| 1228 | if (!decls.fVars.size()) { |
| 1229 | iter = b.fNodes.erase(iter); |
| 1230 | } else { |
| 1231 | ++iter; |
| 1232 | } |
| 1233 | break; |
| 1234 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1235 | default: |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1236 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1237 | break; |
| 1238 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1239 | } else { |
| 1240 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1245 | // check for missing return |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1246 | if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1247 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1248 | this->error(f.fOffset, String("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1253 | void Compiler::registerExternalValue(ExternalValue* value) { |
| 1254 | fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value); |
| 1255 | } |
| 1256 | |
| 1257 | Symbol* Compiler::takeOwnership(std::unique_ptr<Symbol> symbol) { |
| 1258 | return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol)); |
| 1259 | } |
| 1260 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1261 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1262 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1263 | fErrorText = ""; |
| 1264 | fErrorCount = 0; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1265 | std::vector<std::unique_ptr<ProgramElement>>* inherited; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1266 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1267 | switch (kind) { |
| 1268 | case Program::kVertex_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1269 | inherited = &fVertexInclude; |
| 1270 | fIRGenerator->fSymbolTable = fVertexSymbolTable; |
| 1271 | fIRGenerator->start(&settings, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | case Program::kFragment_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1274 | inherited = &fFragmentInclude; |
| 1275 | fIRGenerator->fSymbolTable = fFragmentSymbolTable; |
| 1276 | fIRGenerator->start(&settings, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1277 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1278 | case Program::kGeometry_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1279 | inherited = &fGeometryInclude; |
| 1280 | fIRGenerator->fSymbolTable = fGeometrySymbolTable; |
| 1281 | fIRGenerator->start(&settings, inherited); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1282 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1283 | case Program::kFragmentProcessor_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1284 | inherited = nullptr; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1285 | fIRGenerator->fSymbolTable = fGpuSymbolTable; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1286 | fIRGenerator->start(&settings, nullptr); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1287 | fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1288 | &elements); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1289 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1290 | break; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1291 | case Program::kPipelineStage_Kind: |
| 1292 | inherited = &fPipelineInclude; |
| 1293 | fIRGenerator->fSymbolTable = fPipelineSymbolTable; |
| 1294 | fIRGenerator->start(&settings, inherited); |
| 1295 | break; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 1296 | case Program::kGeneric_Kind: |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1297 | inherited = &fInterpreterInclude; |
| 1298 | fIRGenerator->fSymbolTable = fInterpreterSymbolTable; |
| 1299 | fIRGenerator->start(&settings, inherited); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 1300 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1301 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1302 | for (auto& element : elements) { |
| 1303 | if (element->fKind == ProgramElement::kEnum_Kind) { |
| 1304 | ((Enum&) *element).fBuiltin = true; |
| 1305 | } |
| 1306 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1307 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1308 | fSource = textPtr.get(); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1309 | fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1310 | auto result = std::unique_ptr<Program>(new Program(kind, |
| 1311 | std::move(textPtr), |
| 1312 | settings, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1313 | fContext, |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1314 | inherited, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1315 | std::move(elements), |
| 1316 | fIRGenerator->fSymbolTable, |
| 1317 | fIRGenerator->fInputs)); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1318 | if (fErrorCount) { |
| 1319 | return nullptr; |
| 1320 | } |
| 1321 | return result; |
| 1322 | } |
| 1323 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1324 | bool Compiler::optimize(Program& program) { |
| 1325 | SkASSERT(!fErrorCount); |
| 1326 | if (!program.fIsOptimized) { |
| 1327 | program.fIsOptimized = true; |
| 1328 | fIRGenerator->fKind = program.fKind; |
| 1329 | fIRGenerator->fSettings = &program.fSettings; |
| 1330 | for (auto& element : program) { |
| 1331 | if (element.fKind == ProgramElement::kFunction_Kind) { |
| 1332 | this->scanCFG((FunctionDefinition&) element); |
| 1333 | } |
| 1334 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1335 | if (program.fKind != Program::kFragmentProcessor_Kind) { |
| 1336 | for (auto iter = program.fElements.begin(); iter != program.fElements.end();) { |
| 1337 | if ((*iter)->fKind == ProgramElement::kVar_Kind) { |
| 1338 | VarDeclarations& vars = (VarDeclarations&) **iter; |
| 1339 | for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) { |
| 1340 | const Variable& var = *((VarDeclaration&) **varIter).fVar; |
| 1341 | if (var.dead()) { |
| 1342 | varIter = vars.fVars.erase(varIter); |
| 1343 | } else { |
| 1344 | ++varIter; |
| 1345 | } |
| 1346 | } |
| 1347 | if (vars.fVars.size() == 0) { |
| 1348 | iter = program.fElements.erase(iter); |
| 1349 | continue; |
| 1350 | } |
| 1351 | } |
| 1352 | ++iter; |
| 1353 | } |
| 1354 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1355 | } |
| 1356 | return fErrorCount == 0; |
| 1357 | } |
| 1358 | |
| 1359 | std::unique_ptr<Program> Compiler::specialize( |
| 1360 | Program& program, |
| 1361 | const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) { |
| 1362 | std::vector<std::unique_ptr<ProgramElement>> elements; |
| 1363 | for (const auto& e : program) { |
| 1364 | elements.push_back(e.clone()); |
| 1365 | } |
| 1366 | Program::Settings settings; |
| 1367 | settings.fCaps = program.fSettings.fCaps; |
| 1368 | for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) { |
| 1369 | settings.fArgs.insert(*iter); |
| 1370 | } |
| 1371 | std::unique_ptr<Program> result(new Program(program.fKind, |
| 1372 | nullptr, |
| 1373 | settings, |
| 1374 | program.fContext, |
| 1375 | program.fInheritedElements, |
| 1376 | std::move(elements), |
| 1377 | program.fSymbols, |
| 1378 | program.fInputs)); |
| 1379 | return result; |
| 1380 | } |
| 1381 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame^] | 1382 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1383 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1384 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
| 1385 | if (!this->optimize(program)) { |
| 1386 | return false; |
| 1387 | } |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1388 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1389 | StringStream buffer; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1390 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1391 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1392 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1393 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1394 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1395 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1396 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1397 | SkASSERT(0 == data.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1398 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1399 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1400 | }; |
| 1401 | tools.SetMessageConsumer(dumpmsg); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1402 | // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1403 | // to the failure to see the validation errors. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1404 | SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1405 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1406 | } |
| 1407 | #else |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1408 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1409 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1410 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1411 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1412 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1413 | return result; |
| 1414 | } |
| 1415 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1416 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1417 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1418 | bool result = this->toSPIRV(program, buffer); |
| 1419 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1420 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1421 | } |
| 1422 | return result; |
| 1423 | } |
| 1424 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1425 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
| 1426 | if (!this->optimize(program)) { |
| 1427 | return false; |
| 1428 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1429 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1430 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1431 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1432 | fSource = nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1433 | return result; |
| 1434 | } |
| 1435 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1436 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1437 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1438 | bool result = this->toGLSL(program, buffer); |
| 1439 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1440 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1441 | } |
| 1442 | return result; |
| 1443 | } |
| 1444 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1445 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
| 1446 | if (!this->optimize(program)) { |
| 1447 | return false; |
| 1448 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1449 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1450 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1451 | return result; |
| 1452 | } |
| 1453 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1454 | bool Compiler::toMetal(Program& program, String* out) { |
| 1455 | if (!this->optimize(program)) { |
| 1456 | return false; |
| 1457 | } |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1458 | StringStream buffer; |
| 1459 | bool result = this->toMetal(program, buffer); |
| 1460 | if (result) { |
| 1461 | *out = buffer.str(); |
| 1462 | } |
| 1463 | return result; |
| 1464 | } |
| 1465 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1466 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
| 1467 | if (!this->optimize(program)) { |
| 1468 | return false; |
| 1469 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1470 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1471 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1472 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1473 | fSource = nullptr; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1474 | return result; |
| 1475 | } |
| 1476 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1477 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
| 1478 | if (!this->optimize(program)) { |
| 1479 | return false; |
| 1480 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1481 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1482 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1483 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1484 | fSource = nullptr; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1485 | return result; |
| 1486 | } |
| 1487 | |
| 1488 | bool Compiler::toPipelineStage(const Program& program, String* out, |
| 1489 | std::vector<FormatArg>* outFormatArgs) { |
| 1490 | SkASSERT(program.fIsOptimized); |
| 1491 | fSource = program.fSource.get(); |
| 1492 | StringStream buffer; |
| 1493 | PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outFormatArgs); |
| 1494 | bool result = cg.generateCode(); |
| 1495 | fSource = nullptr; |
| 1496 | if (result) { |
| 1497 | *out = buffer.str(); |
| 1498 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1499 | return result; |
| 1500 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1501 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame^] | 1502 | #endif |
| 1503 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1504 | std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) { |
| 1505 | if (!this->optimize(program)) { |
| 1506 | return nullptr; |
| 1507 | } |
| 1508 | std::unique_ptr<ByteCode> result(new ByteCode()); |
| 1509 | ByteCodeGenerator cg(fContext.get(), &program, this, result.get()); |
| 1510 | if (cg.generateCode()) { |
| 1511 | return result; |
| 1512 | } |
| 1513 | return nullptr; |
| 1514 | } |
| 1515 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1516 | const char* Compiler::OperatorName(Token::Kind kind) { |
| 1517 | switch (kind) { |
| 1518 | case Token::PLUS: return "+"; |
| 1519 | case Token::MINUS: return "-"; |
| 1520 | case Token::STAR: return "*"; |
| 1521 | case Token::SLASH: return "/"; |
| 1522 | case Token::PERCENT: return "%"; |
| 1523 | case Token::SHL: return "<<"; |
| 1524 | case Token::SHR: return ">>"; |
| 1525 | case Token::LOGICALNOT: return "!"; |
| 1526 | case Token::LOGICALAND: return "&&"; |
| 1527 | case Token::LOGICALOR: return "||"; |
| 1528 | case Token::LOGICALXOR: return "^^"; |
| 1529 | case Token::BITWISENOT: return "~"; |
| 1530 | case Token::BITWISEAND: return "&"; |
| 1531 | case Token::BITWISEOR: return "|"; |
| 1532 | case Token::BITWISEXOR: return "^"; |
| 1533 | case Token::EQ: return "="; |
| 1534 | case Token::EQEQ: return "=="; |
| 1535 | case Token::NEQ: return "!="; |
| 1536 | case Token::LT: return "<"; |
| 1537 | case Token::GT: return ">"; |
| 1538 | case Token::LTEQ: return "<="; |
| 1539 | case Token::GTEQ: return ">="; |
| 1540 | case Token::PLUSEQ: return "+="; |
| 1541 | case Token::MINUSEQ: return "-="; |
| 1542 | case Token::STAREQ: return "*="; |
| 1543 | case Token::SLASHEQ: return "/="; |
| 1544 | case Token::PERCENTEQ: return "%="; |
| 1545 | case Token::SHLEQ: return "<<="; |
| 1546 | case Token::SHREQ: return ">>="; |
| 1547 | case Token::LOGICALANDEQ: return "&&="; |
| 1548 | case Token::LOGICALOREQ: return "||="; |
| 1549 | case Token::LOGICALXOREQ: return "^^="; |
| 1550 | case Token::BITWISEANDEQ: return "&="; |
| 1551 | case Token::BITWISEOREQ: return "|="; |
| 1552 | case Token::BITWISEXOREQ: return "^="; |
| 1553 | case Token::PLUSPLUS: return "++"; |
| 1554 | case Token::MINUSMINUS: return "--"; |
| 1555 | case Token::COMMA: return ","; |
| 1556 | default: |
| 1557 | ABORT("unsupported operator: %d\n", kind); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | bool Compiler::IsAssignment(Token::Kind op) { |
| 1563 | switch (op) { |
| 1564 | case Token::EQ: // fall through |
| 1565 | case Token::PLUSEQ: // fall through |
| 1566 | case Token::MINUSEQ: // fall through |
| 1567 | case Token::STAREQ: // fall through |
| 1568 | case Token::SLASHEQ: // fall through |
| 1569 | case Token::PERCENTEQ: // fall through |
| 1570 | case Token::SHLEQ: // fall through |
| 1571 | case Token::SHREQ: // fall through |
| 1572 | case Token::BITWISEOREQ: // fall through |
| 1573 | case Token::BITWISEXOREQ: // fall through |
| 1574 | case Token::BITWISEANDEQ: // fall through |
| 1575 | case Token::LOGICALOREQ: // fall through |
| 1576 | case Token::LOGICALXOREQ: // fall through |
| 1577 | case Token::LOGICALANDEQ: |
| 1578 | return true; |
| 1579 | default: |
| 1580 | return false; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | Position Compiler::position(int offset) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1585 | SkASSERT(fSource); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1586 | int line = 1; |
| 1587 | int column = 1; |
| 1588 | for (int i = 0; i < offset; i++) { |
| 1589 | if ((*fSource)[i] == '\n') { |
| 1590 | ++line; |
| 1591 | column = 1; |
| 1592 | } |
| 1593 | else { |
| 1594 | ++column; |
| 1595 | } |
| 1596 | } |
| 1597 | return Position(line, column); |
| 1598 | } |
| 1599 | |
| 1600 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1601 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1602 | Position pos = this->position(offset); |
| 1603 | fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1606 | String Compiler::errorText() { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1607 | this->writeErrorCount(); |
| 1608 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1609 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1610 | return result; |
| 1611 | } |
| 1612 | |
| 1613 | void Compiler::writeErrorCount() { |
| 1614 | if (fErrorCount) { |
| 1615 | fErrorText += to_string(fErrorCount) + " error"; |
| 1616 | if (fErrorCount > 1) { |
| 1617 | fErrorText += "s"; |
| 1618 | } |
| 1619 | fErrorText += "\n"; |
| 1620 | } |
| 1621 | } |
| 1622 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1623 | } // namespace |