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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_COMPILER |
| 9 | #define SKSL_COMPILER |
| 10 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include <set> |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 12 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | #include "ir/SkSLProgram.h" |
| 15 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 16 | #include "SkSLCFGGenerator.h" |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 17 | #include "SkSLContext.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 | #include "SkSLErrorReporter.h" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 19 | #include "SkSLIRGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 21 | #define SK_FRAGCOLOR_BUILTIN 10001 |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 22 | #define SK_IN_BUILTIN 10002 |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 23 | #define SK_FRAGCOORD_BUILTIN 15 |
| 24 | #define SK_VERTEXID_BUILTIN 5 |
| 25 | #define SK_CLIPDISTANCE_BUILTIN 3 |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 26 | #define SK_INVOCATIONID_BUILTIN 8 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 27 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | namespace SkSL { |
| 29 | |
| 30 | class IRGenerator; |
| 31 | |
| 32 | /** |
| 33 | * Main compiler entry point. This is a traditional compiler design which first parses the .sksl |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 34 | * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 36 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 37 | * |
| 38 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | */ |
| 40 | class Compiler : public ErrorReporter { |
| 41 | public: |
| 42 | Compiler(); |
| 43 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 44 | ~Compiler() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 45 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 46 | std::unique_ptr<Program> convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 47 | const Program::Settings& settings); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 49 | bool toSPIRV(const Program& program, OutputStream& out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 50 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 51 | bool toSPIRV(const Program& program, String* out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 52 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 53 | bool toGLSL(const Program& program, OutputStream& out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 54 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 55 | bool toGLSL(const Program& program, String* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 57 | void error(Position position, String msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 58 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 59 | String errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 60 | |
| 61 | void writeErrorCount(); |
| 62 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 63 | int errorCount() override { |
| 64 | return fErrorCount; |
| 65 | } |
| 66 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 67 | private: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 68 | void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 69 | DefinitionMap* definitions); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 70 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 72 | |
| 73 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 74 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 75 | void computeDataFlow(CFG* cfg); |
| 76 | |
| 77 | /** |
| 78 | * Simplifies the expression pointed to by iter (in both the IR and CFG structures), if |
| 79 | * possible. |
| 80 | */ |
| 81 | void simplifyExpression(DefinitionMap& definitions, |
| 82 | BasicBlock& b, |
| 83 | std::vector<BasicBlock::Node>::iterator* iter, |
| 84 | std::unordered_set<const Variable*>* undefinedVariables, |
| 85 | bool* outUpdated, |
| 86 | bool* outNeedsRescan); |
| 87 | |
| 88 | /** |
| 89 | * Simplifies the statement pointed to by iter (in both the IR and CFG structures), if |
| 90 | * possible. |
| 91 | */ |
| 92 | void simplifyStatement(DefinitionMap& definitions, |
| 93 | BasicBlock& b, |
| 94 | std::vector<BasicBlock::Node>::iterator* iter, |
| 95 | std::unordered_set<const Variable*>* undefinedVariables, |
| 96 | bool* outUpdated, |
| 97 | bool* outNeedsRescan); |
| 98 | |
| 99 | void scanCFG(FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 100 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 101 | void internalConvertProgram(String text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 102 | Modifiers::Flag* defaultPrecision, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 103 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 104 | |
| 105 | std::shared_ptr<SymbolTable> fTypes; |
| 106 | IRGenerator* fIRGenerator; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 107 | String fSkiaVertText; // FIXME store parsed version instead |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 108 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 109 | Context fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 110 | int fErrorCount; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 111 | String fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace |
| 115 | |
| 116 | #endif |