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 | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 10 | #include "ast/SkSLASTPrecision.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include "SkSLCFGGenerator.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 12 | #include "SkSLGLSLCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include "SkSLIRGenerator.h" |
| 14 | #include "SkSLParser.h" |
| 15 | #include "SkSLSPIRVCodeGenerator.h" |
| 16 | #include "ir/SkSLExpression.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 17 | #include "ir/SkSLExpressionStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 19 | #include "ir/SkSLModifiersDeclaration.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 20 | #include "ir/SkSLNop.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | #include "ir/SkSLSymbolTable.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 22 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 23 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 24 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 26 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 27 | #include "spirv-tools/libspirv.hpp" |
| 28 | #endif |
| 29 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 30 | #define STRINGIFY(x) #x |
| 31 | |
| 32 | // include the built-in shader symbols as static strings |
| 33 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 34 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | #include "sksl.include" |
| 36 | ; |
| 37 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #include "sksl_vert.include" |
| 40 | ; |
| 41 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 42 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | #include "sksl_frag.include" |
| 44 | ; |
| 45 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 46 | static const char* SKSL_GEOM_INCLUDE = |
| 47 | #include "sksl_geom.include" |
| 48 | ; |
| 49 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | namespace SkSL { |
| 51 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 52 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | : fErrorCount(0) { |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 54 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this)); |
| 55 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 56 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 58 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 59 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 60 | ADD_TYPE(Void); |
| 61 | ADD_TYPE(Float); |
| 62 | ADD_TYPE(Vec2); |
| 63 | ADD_TYPE(Vec3); |
| 64 | ADD_TYPE(Vec4); |
| 65 | ADD_TYPE(Double); |
| 66 | ADD_TYPE(DVec2); |
| 67 | ADD_TYPE(DVec3); |
| 68 | ADD_TYPE(DVec4); |
| 69 | ADD_TYPE(Int); |
| 70 | ADD_TYPE(IVec2); |
| 71 | ADD_TYPE(IVec3); |
| 72 | ADD_TYPE(IVec4); |
| 73 | ADD_TYPE(UInt); |
| 74 | ADD_TYPE(UVec2); |
| 75 | ADD_TYPE(UVec3); |
| 76 | ADD_TYPE(UVec4); |
| 77 | ADD_TYPE(Bool); |
| 78 | ADD_TYPE(BVec2); |
| 79 | ADD_TYPE(BVec3); |
| 80 | ADD_TYPE(BVec4); |
| 81 | ADD_TYPE(Mat2x2); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 82 | types->addWithoutOwnership(String("mat2x2"), fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | ADD_TYPE(Mat2x3); |
| 84 | ADD_TYPE(Mat2x4); |
| 85 | ADD_TYPE(Mat3x2); |
| 86 | ADD_TYPE(Mat3x3); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 87 | types->addWithoutOwnership(String("mat3x3"), fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | ADD_TYPE(Mat3x4); |
| 89 | ADD_TYPE(Mat4x2); |
| 90 | ADD_TYPE(Mat4x3); |
| 91 | ADD_TYPE(Mat4x4); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 92 | types->addWithoutOwnership(String("mat4x4"), fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | ADD_TYPE(GenType); |
| 94 | ADD_TYPE(GenDType); |
| 95 | ADD_TYPE(GenIType); |
| 96 | ADD_TYPE(GenUType); |
| 97 | ADD_TYPE(GenBType); |
| 98 | ADD_TYPE(Mat); |
| 99 | ADD_TYPE(Vec); |
| 100 | ADD_TYPE(GVec); |
| 101 | ADD_TYPE(GVec2); |
| 102 | ADD_TYPE(GVec3); |
| 103 | ADD_TYPE(GVec4); |
| 104 | ADD_TYPE(DVec); |
| 105 | ADD_TYPE(IVec); |
| 106 | ADD_TYPE(UVec); |
| 107 | ADD_TYPE(BVec); |
| 108 | |
| 109 | ADD_TYPE(Sampler1D); |
| 110 | ADD_TYPE(Sampler2D); |
| 111 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 112 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | ADD_TYPE(SamplerCube); |
| 114 | ADD_TYPE(Sampler2DRect); |
| 115 | ADD_TYPE(Sampler1DArray); |
| 116 | ADD_TYPE(Sampler2DArray); |
| 117 | ADD_TYPE(SamplerCubeArray); |
| 118 | ADD_TYPE(SamplerBuffer); |
| 119 | ADD_TYPE(Sampler2DMS); |
| 120 | ADD_TYPE(Sampler2DMSArray); |
| 121 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 122 | ADD_TYPE(ISampler2D); |
| 123 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 124 | ADD_TYPE(Image2D); |
| 125 | ADD_TYPE(IImage2D); |
| 126 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 127 | ADD_TYPE(SubpassInput); |
| 128 | ADD_TYPE(SubpassInputMS); |
| 129 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | ADD_TYPE(GSampler1D); |
| 131 | ADD_TYPE(GSampler2D); |
| 132 | ADD_TYPE(GSampler3D); |
| 133 | ADD_TYPE(GSamplerCube); |
| 134 | ADD_TYPE(GSampler2DRect); |
| 135 | ADD_TYPE(GSampler1DArray); |
| 136 | ADD_TYPE(GSampler2DArray); |
| 137 | ADD_TYPE(GSamplerCubeArray); |
| 138 | ADD_TYPE(GSamplerBuffer); |
| 139 | ADD_TYPE(GSampler2DMS); |
| 140 | ADD_TYPE(GSampler2DMSArray); |
| 141 | |
| 142 | ADD_TYPE(Sampler1DShadow); |
| 143 | ADD_TYPE(Sampler2DShadow); |
| 144 | ADD_TYPE(SamplerCubeShadow); |
| 145 | ADD_TYPE(Sampler2DRectShadow); |
| 146 | ADD_TYPE(Sampler1DArrayShadow); |
| 147 | ADD_TYPE(Sampler2DArrayShadow); |
| 148 | ADD_TYPE(SamplerCubeArrayShadow); |
| 149 | ADD_TYPE(GSampler2DArrayShadow); |
| 150 | ADD_TYPE(GSamplerCubeArrayShadow); |
| 151 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 152 | String skCapsName("sk_Caps"); |
| 153 | Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName, |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 154 | *fContext.fSkCaps_Type, Variable::kGlobal_Storage); |
| 155 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 156 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 157 | Modifiers::Flag ignored1; |
| 158 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 159 | this->internalConvertProgram(String(SKSL_INCLUDE), &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 160 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | ASSERT(!fErrorCount); |
| 162 | } |
| 163 | |
| 164 | Compiler::~Compiler() { |
| 165 | delete fIRGenerator; |
| 166 | } |
| 167 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 168 | // 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] | 169 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 170 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 171 | switch (lvalue->fKind) { |
| 172 | case Expression::kVariableReference_Kind: { |
| 173 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 174 | if (var.fStorage == Variable::kLocal_Storage) { |
| 175 | (*definitions)[&var] = expr; |
| 176 | } |
| 177 | break; |
| 178 | } |
| 179 | case Expression::kSwizzle_Kind: |
| 180 | // We consider the variable written to as long as at least some of its components have |
| 181 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 182 | // 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] | 183 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 184 | // 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] | 185 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 186 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 187 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 188 | definitions); |
| 189 | break; |
| 190 | case Expression::kIndex_Kind: |
| 191 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 192 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 193 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 194 | definitions); |
| 195 | break; |
| 196 | case Expression::kFieldAccess_Kind: |
| 197 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 198 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 199 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 200 | definitions); |
| 201 | break; |
| 202 | default: |
| 203 | // not an lvalue, can't happen |
| 204 | ASSERT(false); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 209 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 210 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 211 | switch (node.fKind) { |
| 212 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 213 | ASSERT(node.expression()); |
| 214 | const Expression* expr = (Expression*) node.expression()->get(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 215 | switch (expr->fKind) { |
| 216 | case Expression::kBinary_Kind: { |
| 217 | BinaryExpression* b = (BinaryExpression*) expr; |
| 218 | if (b->fOperator == Token::EQ) { |
| 219 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
| 220 | } else if (Token::IsAssignment(b->fOperator)) { |
| 221 | this->addDefinition( |
| 222 | b->fLeft.get(), |
| 223 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 224 | definitions); |
| 225 | |
| 226 | } |
| 227 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 228 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 229 | case Expression::kPrefix_Kind: { |
| 230 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 231 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 232 | this->addDefinition( |
| 233 | p->fOperand.get(), |
| 234 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 235 | definitions); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | case Expression::kPostfix_Kind: { |
| 240 | const PostfixExpression* p = (PostfixExpression*) expr; |
| 241 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 242 | this->addDefinition( |
| 243 | p->fOperand.get(), |
| 244 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 245 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 249 | case Expression::kVariableReference_Kind: { |
| 250 | const VariableReference* v = (VariableReference*) expr; |
| 251 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 252 | this->addDefinition( |
| 253 | v, |
| 254 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 255 | definitions); |
| 256 | } |
| 257 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 258 | default: |
| 259 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 260 | } |
| 261 | break; |
| 262 | } |
| 263 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 264 | const Statement* stmt = (Statement*) node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 265 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 266 | VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 267 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 268 | if (decl->fValue) { |
| 269 | (*definitions)[decl->fVar] = &decl->fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 279 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 280 | |
| 281 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 282 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 283 | for (const BasicBlock::Node& n : block.fNodes) { |
| 284 | this->addDefinitions(n, &after); |
| 285 | } |
| 286 | |
| 287 | // propagate definitions to exits |
| 288 | for (BlockId exitId : block.fExits) { |
| 289 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 290 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 291 | std::unique_ptr<Expression>* e1 = pair.second; |
| 292 | auto found = exit.fBefore.find(pair.first); |
| 293 | if (found == exit.fBefore.end()) { |
| 294 | // exit has no definition for it, just copy it |
| 295 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 296 | exit.fBefore[pair.first] = e1; |
| 297 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 298 | // exit has a (possibly different) value already defined |
| 299 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 300 | if (e1 != e2) { |
| 301 | // definition has changed, merge and add exit block to worklist |
| 302 | workList->insert(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 303 | if (e1 && e2) { |
| 304 | exit.fBefore[pair.first] = |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 305 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 306 | } else { |
| 307 | exit.fBefore[pair.first] = nullptr; |
| 308 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 316 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 317 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 318 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 319 | for (const auto& block : cfg.fBlocks) { |
| 320 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 321 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 322 | ASSERT(node.statement()); |
| 323 | const Statement* s = node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 324 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 325 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 326 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 327 | result[decl->fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 328 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return result; |
| 334 | } |
| 335 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 336 | /** |
| 337 | * Returns true if assigning to this lvalue has no effect. |
| 338 | */ |
| 339 | static bool is_dead(const Expression& lvalue) { |
| 340 | switch (lvalue.fKind) { |
| 341 | case Expression::kVariableReference_Kind: |
| 342 | return ((VariableReference&) lvalue).fVariable.dead(); |
| 343 | case Expression::kSwizzle_Kind: |
| 344 | return is_dead(*((Swizzle&) lvalue).fBase); |
| 345 | case Expression::kFieldAccess_Kind: |
| 346 | return is_dead(*((FieldAccess&) lvalue).fBase); |
| 347 | case Expression::kIndex_Kind: { |
| 348 | const IndexExpression& idx = (IndexExpression&) lvalue; |
| 349 | return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects(); |
| 350 | } |
| 351 | default: |
| 352 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
| 353 | } |
| 354 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 355 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 356 | /** |
| 357 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 358 | * to a dead target and lack of side effects on the left hand side. |
| 359 | */ |
| 360 | static bool dead_assignment(const BinaryExpression& b) { |
| 361 | if (!Token::IsAssignment(b.fOperator)) { |
| 362 | return false; |
| 363 | } |
| 364 | return is_dead(*b.fLeft); |
| 365 | } |
| 366 | |
| 367 | void Compiler::computeDataFlow(CFG* cfg) { |
| 368 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 369 | std::set<BlockId> workList; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 370 | for (BlockId i = 0; i < cfg->fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 371 | workList.insert(i); |
| 372 | } |
| 373 | while (workList.size()) { |
| 374 | BlockId next = *workList.begin(); |
| 375 | workList.erase(workList.begin()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 376 | this->scanCFG(cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 377 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 382 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 383 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 384 | * need to be regenerated). |
| 385 | */ |
| 386 | bool try_replace_expression(BasicBlock* b, |
| 387 | std::vector<BasicBlock::Node>::iterator* iter, |
| 388 | std::unique_ptr<Expression>* newExpression) { |
| 389 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 390 | if (!b->tryRemoveExpression(iter)) { |
| 391 | *target = std::move(*newExpression); |
| 392 | return false; |
| 393 | } |
| 394 | *target = std::move(*newExpression); |
| 395 | return b->tryInsertExpression(iter, target); |
| 396 | } |
| 397 | |
| 398 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 399 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 400 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 401 | */ |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 402 | bool is_constant(const Expression& expr, double value) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 403 | switch (expr.fKind) { |
| 404 | case Expression::kIntLiteral_Kind: |
| 405 | return ((IntLiteral&) expr).fValue == value; |
| 406 | case Expression::kFloatLiteral_Kind: |
| 407 | return ((FloatLiteral&) expr).fValue == value; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 408 | case Expression::kConstructor_Kind: { |
| 409 | Constructor& c = (Constructor&) expr; |
| 410 | if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) { |
| 411 | for (int i = 0; i < c.fType.columns(); ++i) { |
| 412 | if (!is_constant(c.getVecComponent(i), value)) { |
| 413 | return false; |
| 414 | } |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 420 | default: |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 427 | * and CFG structures). |
| 428 | */ |
| 429 | void delete_left(BasicBlock* b, |
| 430 | std::vector<BasicBlock::Node>::iterator* iter, |
| 431 | bool* outUpdated, |
| 432 | bool* outNeedsRescan) { |
| 433 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 434 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 435 | ASSERT((*target)->fKind == Expression::kBinary_Kind); |
| 436 | BinaryExpression& bin = (BinaryExpression&) **target; |
| 437 | bool result; |
| 438 | if (bin.fOperator == Token::EQ) { |
| 439 | result = !b->tryRemoveLValueBefore(iter, bin.fLeft.get()); |
| 440 | } else { |
| 441 | result = !b->tryRemoveExpressionBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 442 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 443 | if (!result) { |
| 444 | *target = std::move(bin.fRight); |
| 445 | *outNeedsRescan = true; |
| 446 | return; |
| 447 | } |
| 448 | --(*iter); |
| 449 | ASSERT((*iter)->expression() == &bin.fRight); |
| 450 | *iter = b->fNodes.erase(*iter); |
| 451 | ASSERT((*iter)->expression() == target); |
| 452 | *target = std::move(bin.fRight); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 457 | * CFG structures). |
| 458 | */ |
| 459 | void delete_right(BasicBlock* b, |
| 460 | std::vector<BasicBlock::Node>::iterator* iter, |
| 461 | bool* outUpdated, |
| 462 | bool* outNeedsRescan) { |
| 463 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 464 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 465 | ASSERT((*target)->fKind == Expression::kBinary_Kind); |
| 466 | BinaryExpression& bin = (BinaryExpression&) **target; |
| 467 | if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) { |
| 468 | *target = std::move(bin.fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 469 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 470 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 471 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 472 | --(*iter); |
| 473 | ASSERT((*iter)->expression() == &bin.fLeft); |
| 474 | *iter = b->fNodes.erase(*iter); |
| 475 | ASSERT((*iter)->expression() == target); |
| 476 | *target = std::move(bin.fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 479 | /** |
| 480 | * Constructs the specified type using a single argument. |
| 481 | */ |
| 482 | static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) { |
| 483 | std::vector<std::unique_ptr<Expression>> args; |
| 484 | args.push_back(std::move(v)); |
| 485 | auto result = std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
| 486 | return result; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 491 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 492 | */ |
| 493 | static void vectorize(BasicBlock* b, |
| 494 | std::vector<BasicBlock::Node>::iterator* iter, |
| 495 | const Type& type, |
| 496 | std::unique_ptr<Expression>* otherExpression, |
| 497 | bool* outUpdated, |
| 498 | bool* outNeedsRescan) { |
| 499 | ASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind); |
| 500 | ASSERT(type.kind() == Type::kVector_Kind); |
| 501 | ASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind); |
| 502 | *outUpdated = true; |
| 503 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 504 | if (!b->tryRemoveExpression(iter)) { |
| 505 | *target = construct(type, std::move(*otherExpression)); |
| 506 | *outNeedsRescan = true; |
| 507 | } else { |
| 508 | *target = construct(type, std::move(*otherExpression)); |
| 509 | if (!b->tryInsertExpression(iter, target)) { |
| 510 | *outNeedsRescan = true; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 517 | * left to yield vec<n>(x). |
| 518 | */ |
| 519 | static void vectorize_left(BasicBlock* b, |
| 520 | std::vector<BasicBlock::Node>::iterator* iter, |
| 521 | bool* outUpdated, |
| 522 | bool* outNeedsRescan) { |
| 523 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 524 | vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 529 | * right to yield vec<n>(y). |
| 530 | */ |
| 531 | static void vectorize_right(BasicBlock* b, |
| 532 | std::vector<BasicBlock::Node>::iterator* iter, |
| 533 | bool* outUpdated, |
| 534 | bool* outNeedsRescan) { |
| 535 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 536 | vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan); |
| 537 | } |
| 538 | |
| 539 | // Mark that an expression which we were writing to is no longer being written to |
| 540 | void clear_write(const Expression& expr) { |
| 541 | switch (expr.fKind) { |
| 542 | case Expression::kVariableReference_Kind: { |
| 543 | ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind); |
| 544 | break; |
| 545 | } |
| 546 | case Expression::kFieldAccess_Kind: |
| 547 | clear_write(*((FieldAccess&) expr).fBase); |
| 548 | break; |
| 549 | case Expression::kSwizzle_Kind: |
| 550 | clear_write(*((Swizzle&) expr).fBase); |
| 551 | break; |
| 552 | case Expression::kIndex_Kind: |
| 553 | clear_write(*((IndexExpression&) expr).fBase); |
| 554 | break; |
| 555 | default: |
| 556 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 561 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 562 | BasicBlock& b, |
| 563 | std::vector<BasicBlock::Node>::iterator* iter, |
| 564 | std::unordered_set<const Variable*>* undefinedVariables, |
| 565 | bool* outUpdated, |
| 566 | bool* outNeedsRescan) { |
| 567 | Expression* expr = (*iter)->expression()->get(); |
| 568 | ASSERT(expr); |
| 569 | if ((*iter)->fConstantPropagation) { |
| 570 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 571 | if (optimized) { |
| 572 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 573 | *outNeedsRescan = true; |
| 574 | } |
| 575 | ASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind); |
| 576 | expr = (*iter)->expression()->get(); |
| 577 | *outUpdated = true; |
| 578 | } |
| 579 | } |
| 580 | switch (expr->fKind) { |
| 581 | case Expression::kVariableReference_Kind: { |
| 582 | const Variable& var = ((VariableReference*) expr)->fVariable; |
| 583 | if (var.fStorage == Variable::kLocal_Storage && !definitions[&var] && |
| 584 | (*undefinedVariables).find(&var) == (*undefinedVariables).end()) { |
| 585 | (*undefinedVariables).insert(&var); |
| 586 | this->error(expr->fPosition, |
| 587 | "'" + var.fName + "' has not been assigned"); |
| 588 | } |
| 589 | break; |
| 590 | } |
| 591 | case Expression::kTernary_Kind: { |
| 592 | TernaryExpression* t = (TernaryExpression*) expr; |
| 593 | if (t->fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 594 | // ternary has a constant test, replace it with either the true or |
| 595 | // false branch |
| 596 | if (((BoolLiteral&) *t->fTest).fValue) { |
| 597 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 598 | } else { |
| 599 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 600 | } |
| 601 | *outUpdated = true; |
| 602 | *outNeedsRescan = true; |
| 603 | } |
| 604 | break; |
| 605 | } |
| 606 | case Expression::kBinary_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 607 | BinaryExpression* bin = (BinaryExpression*) expr; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame^] | 608 | if (dead_assignment(*bin)) { |
| 609 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 610 | break; |
| 611 | } |
| 612 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 613 | if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) && |
| 614 | (bin->fLeft->fType.kind() != Type::kVector_Kind)) || |
| 615 | ((bin->fRight->fType.kind() != Type::kScalar_Kind) && |
| 616 | (bin->fRight->fType.kind() != Type::kVector_Kind))) { |
| 617 | break; |
| 618 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 619 | switch (bin->fOperator) { |
| 620 | case Token::STAR: |
| 621 | if (is_constant(*bin->fLeft, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 622 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 623 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 624 | // vec4(1) * x -> vec4(x) |
| 625 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 626 | } else { |
| 627 | // 1 * x -> x |
| 628 | // 1 * vec4(x) -> vec4(x) |
| 629 | // vec4(1) * vec4(x) -> vec4(x) |
| 630 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 631 | } |
| 632 | } |
| 633 | else if (is_constant(*bin->fLeft, 0)) { |
| 634 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 635 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 636 | // 0 * vec4(x) -> vec4(0) |
| 637 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 638 | } else { |
| 639 | // 0 * x -> 0 |
| 640 | // vec4(0) * x -> vec4(0) |
| 641 | // vec4(0) * vec4(x) -> vec4(0) |
| 642 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 643 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 644 | } |
| 645 | else if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 646 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 647 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 648 | // x * vec4(1) -> vec4(x) |
| 649 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 650 | } else { |
| 651 | // x * 1 -> x |
| 652 | // vec4(x) * 1 -> vec4(x) |
| 653 | // vec4(x) * vec4(1) -> vec4(x) |
| 654 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 655 | } |
| 656 | } |
| 657 | else if (is_constant(*bin->fRight, 0)) { |
| 658 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 659 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 660 | // vec4(x) * 0 -> vec4(0) |
| 661 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 662 | } else { |
| 663 | // x * 0 -> 0 |
| 664 | // x * vec4(0) -> vec4(0) |
| 665 | // vec4(x) * vec4(0) -> vec4(0) |
| 666 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 667 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 668 | } |
| 669 | break; |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 670 | case Token::PLUS: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 671 | if (is_constant(*bin->fLeft, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 672 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 673 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 674 | // vec4(0) + x -> vec4(x) |
| 675 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 676 | } else { |
| 677 | // 0 + x -> x |
| 678 | // 0 + vec4(x) -> vec4(x) |
| 679 | // vec4(0) + vec4(x) -> vec4(x) |
| 680 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 681 | } |
| 682 | } else if (is_constant(*bin->fRight, 0)) { |
| 683 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 684 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 685 | // x + vec4(0) -> vec4(x) |
| 686 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 687 | } else { |
| 688 | // x + 0 -> x |
| 689 | // vec4(x) + 0 -> vec4(x) |
| 690 | // vec4(x) + vec4(0) -> vec4(x) |
| 691 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 692 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 693 | } |
| 694 | break; |
| 695 | case Token::MINUS: |
| 696 | if (is_constant(*bin->fRight, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 697 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 698 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 699 | // x - vec4(0) -> vec4(x) |
| 700 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 701 | } else { |
| 702 | // x - 0 -> x |
| 703 | // vec4(x) - 0 -> vec4(x) |
| 704 | // vec4(x) - vec4(0) -> vec4(x) |
| 705 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 706 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 707 | } |
| 708 | break; |
| 709 | case Token::SLASH: |
| 710 | if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 711 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 712 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 713 | // x / vec4(1) -> vec4(x) |
| 714 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 715 | } else { |
| 716 | // x / 1 -> x |
| 717 | // vec4(x) / 1 -> vec4(x) |
| 718 | // vec4(x) / vec4(1) -> vec4(x) |
| 719 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 720 | } |
| 721 | } else if (is_constant(*bin->fLeft, 0)) { |
| 722 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 723 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 724 | // 0 / vec4(x) -> vec4(0) |
| 725 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 726 | } else { |
| 727 | // 0 / x -> 0 |
| 728 | // vec4(0) / x -> vec4(0) |
| 729 | // vec4(0) / vec4(x) -> vec4(0) |
| 730 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 731 | } |
| 732 | } |
| 733 | break; |
| 734 | case Token::PLUSEQ: |
| 735 | if (is_constant(*bin->fRight, 0)) { |
| 736 | clear_write(*bin->fLeft); |
| 737 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 738 | } |
| 739 | break; |
| 740 | case Token::MINUSEQ: |
| 741 | if (is_constant(*bin->fRight, 0)) { |
| 742 | clear_write(*bin->fLeft); |
| 743 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 744 | } |
| 745 | break; |
| 746 | case Token::STAREQ: |
| 747 | if (is_constant(*bin->fRight, 1)) { |
| 748 | clear_write(*bin->fLeft); |
| 749 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 750 | } |
| 751 | break; |
| 752 | case Token::SLASHEQ: |
| 753 | if (is_constant(*bin->fRight, 1)) { |
| 754 | clear_write(*bin->fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 755 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 756 | } |
| 757 | break; |
| 758 | default: |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | default: |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
| 768 | BasicBlock& b, |
| 769 | std::vector<BasicBlock::Node>::iterator* iter, |
| 770 | std::unordered_set<const Variable*>* undefinedVariables, |
| 771 | bool* outUpdated, |
| 772 | bool* outNeedsRescan) { |
| 773 | Statement* stmt = (*iter)->statement()->get(); |
| 774 | switch (stmt->fKind) { |
| 775 | case Statement::kVarDeclarations_Kind: { |
| 776 | VarDeclarations& vd = *((VarDeclarationsStatement&) *stmt).fDeclaration; |
| 777 | for (auto varIter = vd.fVars.begin(); varIter != vd.fVars.end(); ) { |
| 778 | const auto& varDecl = **varIter; |
| 779 | if (varDecl.fVar->dead() && |
| 780 | (!varDecl.fValue || |
| 781 | !varDecl.fValue->hasSideEffects())) { |
| 782 | if (varDecl.fValue) { |
| 783 | ASSERT((*iter)->statement()->get() == stmt); |
| 784 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 785 | *outNeedsRescan = true; |
| 786 | } |
| 787 | } |
| 788 | varIter = vd.fVars.erase(varIter); |
| 789 | *outUpdated = true; |
| 790 | } else { |
| 791 | ++varIter; |
| 792 | } |
| 793 | } |
| 794 | if (vd.fVars.size() == 0) { |
| 795 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | case Statement::kIf_Kind: { |
| 800 | IfStatement& i = (IfStatement&) *stmt; |
| 801 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 802 | // else block doesn't do anything, remove it |
| 803 | i.fIfFalse.reset(); |
| 804 | *outUpdated = true; |
| 805 | *outNeedsRescan = true; |
| 806 | } |
| 807 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 808 | // if block doesn't do anything, no else block |
| 809 | if (i.fTest->hasSideEffects()) { |
| 810 | // test has side effects, keep it |
| 811 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 812 | new ExpressionStatement(std::move(i.fTest)))); |
| 813 | } else { |
| 814 | // no if, no else, no test side effects, kill the whole if |
| 815 | // statement |
| 816 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 817 | } |
| 818 | *outUpdated = true; |
| 819 | *outNeedsRescan = true; |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | case Statement::kExpression_Kind: { |
| 824 | ExpressionStatement& e = (ExpressionStatement&) *stmt; |
| 825 | ASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 826 | if (!e.fExpression->hasSideEffects()) { |
| 827 | // Expression statement with no side effects, kill it |
| 828 | if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) { |
| 829 | *outNeedsRescan = true; |
| 830 | } |
| 831 | ASSERT((*iter)->statement()->get() == stmt); |
| 832 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 833 | *outUpdated = true; |
| 834 | } |
| 835 | break; |
| 836 | } |
| 837 | default: |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | void Compiler::scanCFG(FunctionDefinition& f) { |
| 843 | CFG cfg = CFGGenerator().getCFG(f); |
| 844 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 845 | |
| 846 | // check for unreachable code |
| 847 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 848 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 849 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 850 | Position p; |
| 851 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 852 | case BasicBlock::Node::kStatement_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 853 | p = (*cfg.fBlocks[i].fNodes[0].statement())->fPosition; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 854 | break; |
| 855 | case BasicBlock::Node::kExpression_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 856 | p = (*cfg.fBlocks[i].fNodes[0].expression())->fPosition; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 857 | break; |
| 858 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 859 | this->error(p, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | if (fErrorCount) { |
| 863 | return; |
| 864 | } |
| 865 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 866 | // check for dead code & undefined variables, perform constant propagation |
| 867 | std::unordered_set<const Variable*> undefinedVariables; |
| 868 | bool updated; |
| 869 | bool needsRescan = false; |
| 870 | do { |
| 871 | if (needsRescan) { |
| 872 | cfg = CFGGenerator().getCFG(f); |
| 873 | this->computeDataFlow(&cfg); |
| 874 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 875 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 876 | |
| 877 | updated = false; |
| 878 | for (BasicBlock& b : cfg.fBlocks) { |
| 879 | DefinitionMap definitions = b.fBefore; |
| 880 | |
| 881 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
| 882 | if (iter->fKind == BasicBlock::Node::kExpression_Kind) { |
| 883 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 884 | &needsRescan); |
| 885 | } else { |
| 886 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
| 887 | &needsRescan); |
| 888 | } |
| 889 | this->addDefinitions(*iter, &definitions); |
| 890 | } |
| 891 | } |
| 892 | } while (updated); |
| 893 | ASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 894 | |
| 895 | // check for missing return |
| 896 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 897 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 898 | this->error(f.fPosition, String("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 903 | void Compiler::internalConvertProgram(String text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 904 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 905 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 906 | Parser parser(text, *fTypes, *this); |
| 907 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 908 | if (fErrorCount) { |
| 909 | return; |
| 910 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 911 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 912 | for (size_t i = 0; i < parsed.size(); i++) { |
| 913 | ASTDeclaration& decl = *parsed[i]; |
| 914 | switch (decl.fKind) { |
| 915 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 916 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 917 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 918 | Variable::kGlobal_Storage); |
| 919 | if (s) { |
| 920 | result->push_back(std::move(s)); |
| 921 | } |
| 922 | break; |
| 923 | } |
| 924 | case ASTDeclaration::kFunction_Kind: { |
| 925 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 926 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 927 | if (!fErrorCount && f) { |
| 928 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 929 | result->push_back(std::move(f)); |
| 930 | } |
| 931 | break; |
| 932 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 933 | case ASTDeclaration::kModifiers_Kind: { |
| 934 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 935 | (ASTModifiersDeclaration&) decl); |
| 936 | if (f) { |
| 937 | result->push_back(std::move(f)); |
| 938 | } |
| 939 | break; |
| 940 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 941 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 942 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 943 | (ASTInterfaceBlock&) decl); |
| 944 | if (i) { |
| 945 | result->push_back(std::move(i)); |
| 946 | } |
| 947 | break; |
| 948 | } |
| 949 | case ASTDeclaration::kExtension_Kind: { |
| 950 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 951 | if (e) { |
| 952 | result->push_back(std::move(e)); |
| 953 | } |
| 954 | break; |
| 955 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 956 | case ASTDeclaration::kPrecision_Kind: { |
| 957 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 958 | break; |
| 959 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 960 | default: |
| 961 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 966 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 967 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 968 | fErrorText = ""; |
| 969 | fErrorCount = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 970 | fIRGenerator->start(&settings); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 971 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 972 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 973 | switch (kind) { |
| 974 | case Program::kVertex_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 975 | this->internalConvertProgram(String(SKSL_VERT_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 976 | break; |
| 977 | case Program::kFragment_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 978 | this->internalConvertProgram(String(SKSL_FRAG_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 979 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 980 | case Program::kGeometry_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 981 | this->internalConvertProgram(String(SKSL_GEOM_INCLUDE), &ignored, &elements); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 982 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 983 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 984 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 985 | Modifiers::Flag defaultPrecision; |
| 986 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 987 | auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext, |
| 988 | std::move(elements), |
| 989 | fIRGenerator->fSymbolTable, |
| 990 | fIRGenerator->fInputs)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 991 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 992 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 993 | if (fErrorCount) { |
| 994 | return nullptr; |
| 995 | } |
| 996 | return result; |
| 997 | } |
| 998 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 999 | bool Compiler::toSPIRV(const Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1000 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1001 | StringStream buffer; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1002 | SPIRVCodeGenerator cg(&fContext, &program, this, &buffer); |
| 1003 | bool result = cg.generateCode(); |
| 1004 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1005 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1006 | ASSERT(0 == buffer.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1007 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1008 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1009 | }; |
| 1010 | tools.SetMessageConsumer(dumpmsg); |
| 1011 | // Verify that the SPIR-V we produced is valid. If this assert fails, check the logs prior |
| 1012 | // to the failure to see the validation errors. |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1013 | ASSERT_RESULT(tools.Validate((const uint32_t*) buffer.data(), buffer.size() / 4)); |
| 1014 | out.write(buffer.data(), buffer.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1015 | } |
| 1016 | #else |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1017 | SPIRVCodeGenerator cg(&fContext, &program, this, &out); |
| 1018 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1019 | #endif |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1020 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1021 | return result; |
| 1022 | } |
| 1023 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1024 | bool Compiler::toSPIRV(const Program& program, String* out) { |
| 1025 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1026 | bool result = this->toSPIRV(program, buffer); |
| 1027 | if (result) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1028 | *out = String(buffer.data(), buffer.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1029 | } |
| 1030 | return result; |
| 1031 | } |
| 1032 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1033 | bool Compiler::toGLSL(const Program& program, OutputStream& out) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1034 | GLSLCodeGenerator cg(&fContext, &program, this, &out); |
| 1035 | bool result = cg.generateCode(); |
| 1036 | this->writeErrorCount(); |
| 1037 | return result; |
| 1038 | } |
| 1039 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1040 | bool Compiler::toGLSL(const Program& program, String* out) { |
| 1041 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1042 | bool result = this->toGLSL(program, buffer); |
| 1043 | if (result) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1044 | *out = String(buffer.data(), buffer.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1045 | } |
| 1046 | return result; |
| 1047 | } |
| 1048 | |
| 1049 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1050 | void Compiler::error(Position position, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1051 | fErrorCount++; |
| 1052 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 1053 | } |
| 1054 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1055 | String Compiler::errorText() { |
| 1056 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1057 | return result; |
| 1058 | } |
| 1059 | |
| 1060 | void Compiler::writeErrorCount() { |
| 1061 | if (fErrorCount) { |
| 1062 | fErrorText += to_string(fErrorCount) + " error"; |
| 1063 | if (fErrorCount > 1) { |
| 1064 | fErrorText += "s"; |
| 1065 | } |
| 1066 | fErrorText += "\n"; |
| 1067 | } |
| 1068 | } |
| 1069 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1070 | } // namespace |