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" |
| 17 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 18 | #include "ir/SkSLModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 20 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 21 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | #include "SkMutex.h" |
| 23 | |
| 24 | #define STRINGIFY(x) #x |
| 25 | |
| 26 | // include the built-in shader symbols as static strings |
| 27 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 28 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | #include "sksl.include" |
| 30 | ; |
| 31 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 32 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | #include "sksl_vert.include" |
| 34 | ; |
| 35 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 36 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | #include "sksl_frag.include" |
| 38 | ; |
| 39 | |
| 40 | namespace SkSL { |
| 41 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 42 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | : fErrorCount(0) { |
| 44 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 45 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 46 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 48 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 49 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | ADD_TYPE(Void); |
| 51 | ADD_TYPE(Float); |
| 52 | ADD_TYPE(Vec2); |
| 53 | ADD_TYPE(Vec3); |
| 54 | ADD_TYPE(Vec4); |
| 55 | ADD_TYPE(Double); |
| 56 | ADD_TYPE(DVec2); |
| 57 | ADD_TYPE(DVec3); |
| 58 | ADD_TYPE(DVec4); |
| 59 | ADD_TYPE(Int); |
| 60 | ADD_TYPE(IVec2); |
| 61 | ADD_TYPE(IVec3); |
| 62 | ADD_TYPE(IVec4); |
| 63 | ADD_TYPE(UInt); |
| 64 | ADD_TYPE(UVec2); |
| 65 | ADD_TYPE(UVec3); |
| 66 | ADD_TYPE(UVec4); |
| 67 | ADD_TYPE(Bool); |
| 68 | ADD_TYPE(BVec2); |
| 69 | ADD_TYPE(BVec3); |
| 70 | ADD_TYPE(BVec4); |
| 71 | ADD_TYPE(Mat2x2); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 72 | types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | ADD_TYPE(Mat2x3); |
| 74 | ADD_TYPE(Mat2x4); |
| 75 | ADD_TYPE(Mat3x2); |
| 76 | ADD_TYPE(Mat3x3); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 77 | types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | ADD_TYPE(Mat3x4); |
| 79 | ADD_TYPE(Mat4x2); |
| 80 | ADD_TYPE(Mat4x3); |
| 81 | ADD_TYPE(Mat4x4); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 82 | types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | ADD_TYPE(GenType); |
| 84 | ADD_TYPE(GenDType); |
| 85 | ADD_TYPE(GenIType); |
| 86 | ADD_TYPE(GenUType); |
| 87 | ADD_TYPE(GenBType); |
| 88 | ADD_TYPE(Mat); |
| 89 | ADD_TYPE(Vec); |
| 90 | ADD_TYPE(GVec); |
| 91 | ADD_TYPE(GVec2); |
| 92 | ADD_TYPE(GVec3); |
| 93 | ADD_TYPE(GVec4); |
| 94 | ADD_TYPE(DVec); |
| 95 | ADD_TYPE(IVec); |
| 96 | ADD_TYPE(UVec); |
| 97 | ADD_TYPE(BVec); |
| 98 | |
| 99 | ADD_TYPE(Sampler1D); |
| 100 | ADD_TYPE(Sampler2D); |
| 101 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 102 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 103 | ADD_TYPE(SamplerCube); |
| 104 | ADD_TYPE(Sampler2DRect); |
| 105 | ADD_TYPE(Sampler1DArray); |
| 106 | ADD_TYPE(Sampler2DArray); |
| 107 | ADD_TYPE(SamplerCubeArray); |
| 108 | ADD_TYPE(SamplerBuffer); |
| 109 | ADD_TYPE(Sampler2DMS); |
| 110 | ADD_TYPE(Sampler2DMSArray); |
| 111 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 112 | ADD_TYPE(ISampler2D); |
| 113 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 114 | ADD_TYPE(Image2D); |
| 115 | ADD_TYPE(IImage2D); |
| 116 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 117 | ADD_TYPE(SubpassInput); |
| 118 | ADD_TYPE(SubpassInputMS); |
| 119 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | ADD_TYPE(GSampler1D); |
| 121 | ADD_TYPE(GSampler2D); |
| 122 | ADD_TYPE(GSampler3D); |
| 123 | ADD_TYPE(GSamplerCube); |
| 124 | ADD_TYPE(GSampler2DRect); |
| 125 | ADD_TYPE(GSampler1DArray); |
| 126 | ADD_TYPE(GSampler2DArray); |
| 127 | ADD_TYPE(GSamplerCubeArray); |
| 128 | ADD_TYPE(GSamplerBuffer); |
| 129 | ADD_TYPE(GSampler2DMS); |
| 130 | ADD_TYPE(GSampler2DMSArray); |
| 131 | |
| 132 | ADD_TYPE(Sampler1DShadow); |
| 133 | ADD_TYPE(Sampler2DShadow); |
| 134 | ADD_TYPE(SamplerCubeShadow); |
| 135 | ADD_TYPE(Sampler2DRectShadow); |
| 136 | ADD_TYPE(Sampler1DArrayShadow); |
| 137 | ADD_TYPE(Sampler2DArrayShadow); |
| 138 | ADD_TYPE(SamplerCubeArrayShadow); |
| 139 | ADD_TYPE(GSampler2DArrayShadow); |
| 140 | ADD_TYPE(GSamplerCubeArrayShadow); |
| 141 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 142 | SkString skCapsName("sk_Caps"); |
| 143 | Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName, |
| 144 | *fContext.fSkCaps_Type, Variable::kGlobal_Storage); |
| 145 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 146 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 147 | Modifiers::Flag ignored1; |
| 148 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 149 | this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 150 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | ASSERT(!fErrorCount); |
| 152 | } |
| 153 | |
| 154 | Compiler::~Compiler() { |
| 155 | delete fIRGenerator; |
| 156 | } |
| 157 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 158 | // 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] | 159 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 160 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 161 | switch (lvalue->fKind) { |
| 162 | case Expression::kVariableReference_Kind: { |
| 163 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 164 | if (var.fStorage == Variable::kLocal_Storage) { |
| 165 | (*definitions)[&var] = expr; |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | case Expression::kSwizzle_Kind: |
| 170 | // We consider the variable written to as long as at least some of its components have |
| 171 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 172 | // 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] | 173 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 174 | // 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] | 175 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 176 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 177 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 178 | definitions); |
| 179 | break; |
| 180 | case Expression::kIndex_Kind: |
| 181 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 182 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 183 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 184 | definitions); |
| 185 | break; |
| 186 | case Expression::kFieldAccess_Kind: |
| 187 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 188 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 189 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 190 | definitions); |
| 191 | break; |
| 192 | default: |
| 193 | // not an lvalue, can't happen |
| 194 | ASSERT(false); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 199 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 200 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 201 | switch (node.fKind) { |
| 202 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 203 | ASSERT(node.fExpression); |
| 204 | const Expression* expr = (Expression*) node.fExpression->get(); |
| 205 | switch (expr->fKind) { |
| 206 | case Expression::kBinary_Kind: { |
| 207 | BinaryExpression* b = (BinaryExpression*) expr; |
| 208 | if (b->fOperator == Token::EQ) { |
| 209 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
| 210 | } else if (Token::IsAssignment(b->fOperator)) { |
| 211 | this->addDefinition( |
| 212 | b->fLeft.get(), |
| 213 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 214 | definitions); |
| 215 | |
| 216 | } |
| 217 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 218 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 219 | case Expression::kPrefix_Kind: { |
| 220 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 221 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 222 | this->addDefinition( |
| 223 | p->fOperand.get(), |
| 224 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 225 | definitions); |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | case Expression::kPostfix_Kind: { |
| 230 | const PostfixExpression* p = (PostfixExpression*) 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 | } |
| 238 | break; |
| 239 | } |
| 240 | default: |
| 241 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 242 | } |
| 243 | break; |
| 244 | } |
| 245 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 246 | const Statement* stmt = (Statement*) node.fStatement; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 247 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 248 | VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
| 249 | for (VarDeclaration& decl : vd->fDeclaration->fVars) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 250 | if (decl.fValue) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 251 | (*definitions)[decl.fVar] = &decl.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 261 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 262 | |
| 263 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 264 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 265 | for (const BasicBlock::Node& n : block.fNodes) { |
| 266 | this->addDefinitions(n, &after); |
| 267 | } |
| 268 | |
| 269 | // propagate definitions to exits |
| 270 | for (BlockId exitId : block.fExits) { |
| 271 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 272 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 273 | std::unique_ptr<Expression>* e1 = pair.second; |
| 274 | auto found = exit.fBefore.find(pair.first); |
| 275 | if (found == exit.fBefore.end()) { |
| 276 | // exit has no definition for it, just copy it |
| 277 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 278 | exit.fBefore[pair.first] = e1; |
| 279 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 280 | // exit has a (possibly different) value already defined |
| 281 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 282 | if (e1 != e2) { |
| 283 | // definition has changed, merge and add exit block to worklist |
| 284 | workList->insert(exitId); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 285 | exit.fBefore[pair.first] = |
| 286 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 294 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 295 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 296 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 297 | for (const auto& block : cfg.fBlocks) { |
| 298 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 299 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 300 | ASSERT(node.fStatement); |
| 301 | const Statement* s = node.fStatement; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 302 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 303 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
| 304 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 305 | result[decl.fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 306 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | void Compiler::scanCFG(const FunctionDefinition& f) { |
| 315 | CFG cfg = CFGGenerator().getCFG(f); |
| 316 | |
| 317 | // compute the data flow |
| 318 | cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg); |
| 319 | std::set<BlockId> workList; |
| 320 | for (BlockId i = 0; i < cfg.fBlocks.size(); i++) { |
| 321 | workList.insert(i); |
| 322 | } |
| 323 | while (workList.size()) { |
| 324 | BlockId next = *workList.begin(); |
| 325 | workList.erase(workList.begin()); |
| 326 | this->scanCFG(&cfg, next, &workList); |
| 327 | } |
| 328 | |
| 329 | // check for unreachable code |
| 330 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 331 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 332 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 333 | Position p; |
| 334 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 335 | case BasicBlock::Node::kStatement_Kind: |
| 336 | p = cfg.fBlocks[i].fNodes[0].fStatement->fPosition; |
| 337 | break; |
| 338 | case BasicBlock::Node::kExpression_Kind: |
| 339 | p = (*cfg.fBlocks[i].fNodes[0].fExpression)->fPosition; |
| 340 | break; |
| 341 | } |
| 342 | this->error(p, SkString("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | if (fErrorCount) { |
| 346 | return; |
| 347 | } |
| 348 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 349 | // check for undefined variables, perform constant propagation |
| 350 | for (BasicBlock& b : cfg.fBlocks) { |
| 351 | DefinitionMap definitions = b.fBefore; |
| 352 | for (BasicBlock::Node& n : b.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 353 | if (n.fKind == BasicBlock::Node::kExpression_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 354 | ASSERT(n.fExpression); |
| 355 | Expression* expr = n.fExpression->get(); |
| 356 | if (n.fConstantPropagation) { |
| 357 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, |
| 358 | definitions); |
| 359 | if (optimized) { |
| 360 | n.fExpression->reset(optimized.release()); |
| 361 | expr = n.fExpression->get(); |
| 362 | } |
| 363 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 364 | if (expr->fKind == Expression::kVariableReference_Kind) { |
| 365 | const Variable& var = ((VariableReference*) expr)->fVariable; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 366 | if (var.fStorage == Variable::kLocal_Storage && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 367 | !definitions[&var]) { |
| 368 | this->error(expr->fPosition, |
| 369 | "'" + var.fName + "' has not been assigned"); |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 370 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | this->addDefinitions(n, &definitions); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // check for missing return |
| 378 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 379 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 380 | this->error(f.fPosition, SkString("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 385 | void Compiler::internalConvertProgram(SkString text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 386 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 387 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 388 | Parser parser(text, *fTypes, *this); |
| 389 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 390 | if (fErrorCount) { |
| 391 | return; |
| 392 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 393 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 394 | for (size_t i = 0; i < parsed.size(); i++) { |
| 395 | ASTDeclaration& decl = *parsed[i]; |
| 396 | switch (decl.fKind) { |
| 397 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 398 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 399 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 400 | Variable::kGlobal_Storage); |
| 401 | if (s) { |
| 402 | result->push_back(std::move(s)); |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | case ASTDeclaration::kFunction_Kind: { |
| 407 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 408 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 409 | if (!fErrorCount && f) { |
| 410 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 411 | result->push_back(std::move(f)); |
| 412 | } |
| 413 | break; |
| 414 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 415 | case ASTDeclaration::kModifiers_Kind: { |
| 416 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 417 | (ASTModifiersDeclaration&) decl); |
| 418 | if (f) { |
| 419 | result->push_back(std::move(f)); |
| 420 | } |
| 421 | break; |
| 422 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 423 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 424 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 425 | (ASTInterfaceBlock&) decl); |
| 426 | if (i) { |
| 427 | result->push_back(std::move(i)); |
| 428 | } |
| 429 | break; |
| 430 | } |
| 431 | case ASTDeclaration::kExtension_Kind: { |
| 432 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 433 | if (e) { |
| 434 | result->push_back(std::move(e)); |
| 435 | } |
| 436 | break; |
| 437 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 438 | case ASTDeclaration::kPrecision_Kind: { |
| 439 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 440 | break; |
| 441 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 442 | default: |
| 443 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 448 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 449 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 450 | fErrorText = ""; |
| 451 | fErrorCount = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 452 | fIRGenerator->start(&settings); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 453 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 454 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 455 | switch (kind) { |
| 456 | case Program::kVertex_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 457 | this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 458 | break; |
| 459 | case Program::kFragment_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 460 | this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 461 | break; |
| 462 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 463 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 464 | Modifiers::Flag defaultPrecision; |
| 465 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 466 | auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext, |
| 467 | std::move(elements), |
| 468 | fIRGenerator->fSymbolTable, |
| 469 | fIRGenerator->fInputs)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 470 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 471 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 472 | if (fErrorCount) { |
| 473 | return nullptr; |
| 474 | } |
| 475 | return result; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | bool Compiler::toSPIRV(const Program& program, SkWStream& out) { |
| 480 | SPIRVCodeGenerator cg(&fContext, &program, this, &out); |
| 481 | bool result = cg.generateCode(); |
| 482 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 483 | return result; |
| 484 | } |
| 485 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 486 | bool Compiler::toSPIRV(const Program& program, SkString* out) { |
| 487 | SkDynamicMemoryWStream buffer; |
| 488 | bool result = this->toSPIRV(program, buffer); |
| 489 | if (result) { |
| 490 | sk_sp<SkData> data(buffer.detachAsData()); |
| 491 | *out = SkString((const char*) data->data(), data->size()); |
| 492 | } |
| 493 | return result; |
| 494 | } |
| 495 | |
| 496 | bool Compiler::toGLSL(const Program& program, SkWStream& out) { |
| 497 | GLSLCodeGenerator cg(&fContext, &program, this, &out); |
| 498 | bool result = cg.generateCode(); |
| 499 | this->writeErrorCount(); |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | bool Compiler::toGLSL(const Program& program, SkString* out) { |
| 504 | SkDynamicMemoryWStream buffer; |
| 505 | bool result = this->toGLSL(program, buffer); |
| 506 | if (result) { |
| 507 | sk_sp<SkData> data(buffer.detachAsData()); |
| 508 | *out = SkString((const char*) data->data(), data->size()); |
| 509 | } |
| 510 | return result; |
| 511 | } |
| 512 | |
| 513 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 514 | void Compiler::error(Position position, SkString msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 515 | fErrorCount++; |
| 516 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 517 | } |
| 518 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 519 | SkString Compiler::errorText() { |
| 520 | SkString result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 521 | return result; |
| 522 | } |
| 523 | |
| 524 | void Compiler::writeErrorCount() { |
| 525 | if (fErrorCount) { |
| 526 | fErrorText += to_string(fErrorCount) + " error"; |
| 527 | if (fErrorCount > 1) { |
| 528 | fErrorText += "s"; |
| 529 | } |
| 530 | fErrorText += "\n"; |
| 531 | } |
| 532 | } |
| 533 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 534 | } // namespace |