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