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 | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 19 | #include "SkSLLexer.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 21 | #define SK_FRAGCOLOR_BUILTIN 10001 |
| 22 | #define SK_IN_BUILTIN 10002 |
| 23 | #define SK_INCOLOR_BUILTIN 10003 |
| 24 | #define SK_OUTCOLOR_BUILTIN 10004 |
| 25 | #define SK_TRANSFORMEDCOORDS2D_BUILTIN 10005 |
| 26 | #define SK_TEXTURESAMPLERS_BUILTIN 10006 |
Ethan Nicholas | 16c1196 | 2018-03-16 12:20:54 -0400 | [diff] [blame] | 27 | #define SK_OUT_BUILTIN 10007 |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 28 | #define SK_LASTFRAGCOLOR_BUILTIN 10008 |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 29 | #define SK_MAIN_X_BUILTIN 10009 |
| 30 | #define SK_MAIN_Y_BUILTIN 10010 |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 31 | #define SK_WIDTH_BUILTIN 10011 |
| 32 | #define SK_HEIGHT_BUILTIN 10012 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 33 | #define SK_FRAGCOORD_BUILTIN 15 |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 34 | #define SK_CLOCKWISE_BUILTIN 17 |
Ethan Nicholas | 9eded2c | 2018-03-22 10:10:44 -0400 | [diff] [blame] | 35 | #define SK_VERTEXID_BUILTIN 42 |
| 36 | #define SK_INSTANCEID_BUILTIN 43 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 37 | #define SK_CLIPDISTANCE_BUILTIN 3 |
| 38 | #define SK_INVOCATIONID_BUILTIN 8 |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 39 | #define SK_POSITION_BUILTIN 0 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 40 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 41 | namespace SkSL { |
| 42 | |
| 43 | class IRGenerator; |
| 44 | |
| 45 | /** |
| 46 | * 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] | 47 | * 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] | 48 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 49 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 50 | * |
| 51 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | */ |
| 53 | class Compiler : public ErrorReporter { |
| 54 | public: |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 55 | static constexpr const char* RTADJUST_NAME = "sk_RTAdjust"; |
| 56 | static constexpr const char* PERVERTEX_NAME = "sk_PerVertex"; |
| 57 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 58 | enum Flags { |
| 59 | kNone_Flags = 0, |
| 60 | // permits static if/switch statements to be used with non-constant tests. This is used when |
| 61 | // producing H and CPP code; the static tests don't have to have constant values *yet*, but |
| 62 | // the generated code will contain a static test which then does have to be a constant. |
| 63 | kPermitInvalidStaticTests_Flag = 1, |
| 64 | }; |
| 65 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 66 | struct FormatArg { |
| 67 | enum class Kind { |
| 68 | kInput, |
| 69 | kOutput, |
| 70 | kUniform, |
| 71 | kChildProcessor |
| 72 | }; |
| 73 | |
| 74 | FormatArg(Kind kind) |
| 75 | : fKind(kind) {} |
| 76 | |
| 77 | FormatArg(Kind kind, int index) |
| 78 | : fKind(kind) |
| 79 | , fIndex(index) {} |
| 80 | |
| 81 | Kind fKind; |
| 82 | |
| 83 | int fIndex; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 86 | Compiler(Flags flags = kNone_Flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 88 | ~Compiler() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 89 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 90 | std::unique_ptr<Program> convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 91 | const Program::Settings& settings); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 92 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 93 | bool optimize(Program& program); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 94 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 95 | std::unique_ptr<Program> specialize(Program& program, |
| 96 | const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 97 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 98 | bool toSPIRV(Program& program, OutputStream& out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 99 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 100 | bool toSPIRV(Program& program, String* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 102 | bool toGLSL(Program& program, OutputStream& out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 103 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 104 | bool toGLSL(Program& program, String* out); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 105 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 106 | bool toMetal(Program& program, OutputStream& out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 107 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 108 | bool toMetal(Program& program, String* out); |
| 109 | |
| 110 | bool toCPP(Program& program, String name, OutputStream& out); |
| 111 | |
| 112 | bool toH(Program& program, String name, OutputStream& out); |
| 113 | |
| 114 | bool toPipelineStage(const Program& program, String* out, |
| 115 | std::vector<FormatArg>* outFormatArgs); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 116 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 117 | void error(int offset, String msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 119 | String errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | |
| 121 | void writeErrorCount(); |
| 122 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 123 | int errorCount() override { |
| 124 | return fErrorCount; |
| 125 | } |
| 126 | |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 127 | Context& context() { |
| 128 | return *fContext; |
| 129 | } |
| 130 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 131 | static const char* OperatorName(Token::Kind token); |
| 132 | |
| 133 | static bool IsAssignment(Token::Kind token); |
| 134 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | private: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 136 | void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 137 | DefinitionMap* definitions); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 138 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 139 | void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 140 | |
| 141 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 142 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 143 | void computeDataFlow(CFG* cfg); |
| 144 | |
| 145 | /** |
| 146 | * Simplifies the expression pointed to by iter (in both the IR and CFG structures), if |
| 147 | * possible. |
| 148 | */ |
| 149 | void simplifyExpression(DefinitionMap& definitions, |
| 150 | BasicBlock& b, |
| 151 | std::vector<BasicBlock::Node>::iterator* iter, |
| 152 | std::unordered_set<const Variable*>* undefinedVariables, |
| 153 | bool* outUpdated, |
| 154 | bool* outNeedsRescan); |
| 155 | |
| 156 | /** |
| 157 | * Simplifies the statement pointed to by iter (in both the IR and CFG structures), if |
| 158 | * possible. |
| 159 | */ |
| 160 | void simplifyStatement(DefinitionMap& definitions, |
| 161 | BasicBlock& b, |
| 162 | std::vector<BasicBlock::Node>::iterator* iter, |
| 163 | std::unordered_set<const Variable*>* undefinedVariables, |
| 164 | bool* outUpdated, |
| 165 | bool* outNeedsRescan); |
| 166 | |
| 167 | void scanCFG(FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 169 | Position position(int offset); |
| 170 | |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 171 | std::vector<std::unique_ptr<ProgramElement>> fVertexInclude; |
| 172 | std::shared_ptr<SymbolTable> fVertexSymbolTable; |
| 173 | std::vector<std::unique_ptr<ProgramElement>> fFragmentInclude; |
| 174 | std::shared_ptr<SymbolTable> fFragmentSymbolTable; |
| 175 | std::vector<std::unique_ptr<ProgramElement>> fGeometryInclude; |
| 176 | std::shared_ptr<SymbolTable> fGeometrySymbolTable; |
| 177 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 178 | std::shared_ptr<SymbolTable> fTypes; |
| 179 | IRGenerator* fIRGenerator; |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 180 | int fFlags; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 182 | const String* fSource; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 183 | std::shared_ptr<Context> fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | int fErrorCount; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 185 | String fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // namespace |
| 189 | |
| 190 | #endif |