blob: 5e7bc9e71b641e051040fb90be67bd5457aaa7cc [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_COMPILER
9#define SKSL_COMPILER
10
ethannicholas22f939e2016-10-13 13:25:34 -070011#include <set>
Ethan Nicholascb670962017-04-20 19:31:52 -040012#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070013#include <vector>
14#include "ir/SkSLProgram.h"
15#include "ir/SkSLSymbolTable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070016#include "SkSLCFGGenerator.h"
ethannicholasd598f792016-07-25 10:08:54 -070017#include "SkSLContext.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070018#include "SkSLErrorReporter.h"
Ethan Nicholas3605ace2016-11-21 15:59:48 -050019#include "SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070020
Ethan Nicholas67d64602017-02-09 10:15:25 -050021#define SK_FRAGCOLOR_BUILTIN 10001
Ethan Nicholas52cad152017-02-16 16:37:32 -050022#define SK_IN_BUILTIN 10002
Ethan Nicholas67d64602017-02-09 10:15:25 -050023#define SK_FRAGCOORD_BUILTIN 15
24#define SK_VERTEXID_BUILTIN 5
25#define SK_CLIPDISTANCE_BUILTIN 3
Ethan Nicholas52cad152017-02-16 16:37:32 -050026#define SK_INVOCATIONID_BUILTIN 8
ethannicholas5961bc92016-10-12 06:39:56 -070027
ethannicholasb3058bd2016-07-01 08:22:01 -070028namespace SkSL {
29
30class IRGenerator;
31
32/**
33 * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
Ethan Nicholas941e7e22016-12-12 15:33:30 -050034 * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
ethannicholasb3058bd2016-07-01 08:22:01 -070035 * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
36 * compiled output.
ethannicholas5961bc92016-10-12 06:39:56 -070037 *
38 * See the README for information about SkSL.
ethannicholasb3058bd2016-07-01 08:22:01 -070039 */
40class Compiler : public ErrorReporter {
41public:
42 Compiler();
43
Brian Salomond3b65972017-03-22 12:05:03 -040044 ~Compiler() override;
ethannicholasb3058bd2016-07-01 08:22:01 -070045
Ethan Nicholas0df1b042017-03-31 13:56:23 -040046 std::unique_ptr<Program> convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -050047 const Program::Settings& settings);
ethannicholasb3058bd2016-07-01 08:22:01 -070048
Ethan Nicholas0df1b042017-03-31 13:56:23 -040049 bool toSPIRV(const Program& program, OutputStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070050
Ethan Nicholas0df1b042017-03-31 13:56:23 -040051 bool toSPIRV(const Program& program, String* out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050052
Ethan Nicholas0df1b042017-03-31 13:56:23 -040053 bool toGLSL(const Program& program, OutputStream& out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050054
Ethan Nicholas0df1b042017-03-31 13:56:23 -040055 bool toGLSL(const Program& program, String* out);
ethannicholasb3058bd2016-07-01 08:22:01 -070056
Ethan Nicholas0df1b042017-03-31 13:56:23 -040057 void error(Position position, String msg) override;
ethannicholasb3058bd2016-07-01 08:22:01 -070058
Ethan Nicholas0df1b042017-03-31 13:56:23 -040059 String errorText();
ethannicholasb3058bd2016-07-01 08:22:01 -070060
61 void writeErrorCount();
62
Ethan Nicholas941e7e22016-12-12 15:33:30 -050063 int errorCount() override {
64 return fErrorCount;
65 }
66
ethannicholasb3058bd2016-07-01 08:22:01 -070067private:
Ethan Nicholas86a43402017-01-19 13:32:00 -050068 void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
69 DefinitionMap* definitions);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050070
Ethan Nicholas86a43402017-01-19 13:32:00 -050071 void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
ethannicholas22f939e2016-10-13 13:25:34 -070072
73 void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
74
Ethan Nicholascb670962017-04-20 19:31:52 -040075 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);
ethannicholasb3058bd2016-07-01 08:22:01 -0700100
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400101 void internalConvertProgram(String text,
ethannicholas5961bc92016-10-12 06:39:56 -0700102 Modifiers::Flag* defaultPrecision,
ethannicholasf789b382016-08-03 12:43:36 -0700103 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -0700104
105 std::shared_ptr<SymbolTable> fTypes;
106 IRGenerator* fIRGenerator;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400107 String fSkiaVertText; // FIXME store parsed version instead
ethannicholasb3058bd2016-07-01 08:22:01 -0700108
ethannicholasd598f792016-07-25 10:08:54 -0700109 Context fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 int fErrorCount;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400111 String fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700112};
113
114} // namespace
115
116#endif