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