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 | |
| 40 | std::unique_ptr<Program> convertProgram(Program::Kind kind, std::string text); |
| 41 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 42 | bool toSPIRV(Program::Kind kind, const std::string& text, std::ostream& out); |
| 43 | |
| 44 | bool toSPIRV(Program::Kind kind, const std::string& text, std::string* out); |
| 45 | |
| 46 | bool toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, std::ostream& out); |
| 47 | |
| 48 | bool toGLSL(Program::Kind kind, const std::string& text, GLCaps caps, std::string* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | |
| 50 | void error(Position position, std::string msg) override; |
| 51 | |
| 52 | std::string errorText(); |
| 53 | |
| 54 | void writeErrorCount(); |
| 55 | |
| 56 | private: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 57 | void addDefinition(const Expression* lvalue, const Expression* expr, |
| 58 | std::unordered_map<const Variable*, const Expression*>* definitions); |
| 59 | |
| 60 | void addDefinitions(const BasicBlock::Node& node, |
| 61 | std::unordered_map<const Variable*, const Expression*>* definitions); |
| 62 | |
| 63 | void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList); |
| 64 | |
| 65 | void scanCFG(const FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | |
| 67 | void internalConvertProgram(std::string text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 68 | Modifiers::Flag* defaultPrecision, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 69 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 70 | |
| 71 | std::shared_ptr<SymbolTable> fTypes; |
| 72 | IRGenerator* fIRGenerator; |
| 73 | std::string fSkiaVertText; // FIXME store parsed version instead |
| 74 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 75 | Context fContext; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | int fErrorCount; |
| 77 | std::string fErrorText; |
| 78 | }; |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | #endif |