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 | */ |
| 7 | |
| 8 | #ifndef SKSL_COMPILER |
| 9 | #define SKSL_COMPILER |
| 10 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include <set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | #include "ir/SkSLProgram.h" |
| 14 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 15 | #include "SkSLCFGGenerator.h" |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 16 | #include "SkSLContext.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | #include "SkSLErrorReporter.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 18 | #include "SkSLGLSLCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 20 | #define SK_FRAGCOLOR_BUILTIN 10001 |
| 21 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
| 24 | class IRGenerator; |
| 25 | |
| 26 | /** |
| 27 | * Main compiler entry point. This is a traditional compiler design which first parses the .sksl |
| 28 | * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to |
| 29 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 30 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 31 | * |
| 32 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | */ |
| 34 | class Compiler : public ErrorReporter { |
| 35 | public: |
| 36 | Compiler(); |
| 37 | |
| 38 | ~Compiler(); |
| 39 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 40 | std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 41 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 42 | bool toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 43 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 44 | bool toSPIRV(Program::Kind kind, const SkString& text, SkString* out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 45 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 46 | bool toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps, |
| 47 | SkWStream& out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 48 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 49 | bool toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps, |
| 50 | SkString* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 51 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 52 | void error(Position position, SkString msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 54 | SkString errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | |
| 56 | void writeErrorCount(); |
| 57 | |
| 58 | private: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 59 | void addDefinition(const Expression* lvalue, const Expression* expr, |
| 60 | std::unordered_map<const Variable*, const Expression*>* definitions); |
| 61 | |
| 62 | void addDefinitions(const BasicBlock::Node& node, |
| 63 | std::unordered_map<const Variable*, const Expression*>* definitions); |
| 64 | |
| 65 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 66 | |
| 67 | void scanCFG(const FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 69 | void internalConvertProgram(SkString text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 70 | Modifiers::Flag* defaultPrecision, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 71 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 72 | |
| 73 | std::shared_ptr<SymbolTable> fTypes; |
| 74 | IRGenerator* fIRGenerator; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 75 | SkString fSkiaVertText; // FIXME store parsed version instead |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 77 | Context fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | int fErrorCount; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 79 | SkString fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | #endif |