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