blob: 497d06dd83c1c9dcb44f17e0fc265047e5fd9ec4 [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 */
7
8#ifndef SKSL_COMPILER
9#define SKSL_COMPILER
10
ethannicholas22f939e2016-10-13 13:25:34 -070011#include <set>
ethannicholasb3058bd2016-07-01 08:22:01 -070012#include <vector>
13#include "ir/SkSLProgram.h"
14#include "ir/SkSLSymbolTable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070015#include "SkSLCFGGenerator.h"
ethannicholasd598f792016-07-25 10:08:54 -070016#include "SkSLContext.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070017#include "SkSLErrorReporter.h"
Ethan Nicholas3605ace2016-11-21 15:59:48 -050018#include "SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070019
Ethan Nicholas67d64602017-02-09 10:15:25 -050020#define SK_FRAGCOLOR_BUILTIN 10001
Ethan Nicholas52cad152017-02-16 16:37:32 -050021#define SK_IN_BUILTIN 10002
Ethan Nicholas67d64602017-02-09 10:15:25 -050022#define SK_FRAGCOORD_BUILTIN 15
23#define SK_VERTEXID_BUILTIN 5
24#define SK_CLIPDISTANCE_BUILTIN 3
Ethan Nicholas52cad152017-02-16 16:37:32 -050025#define SK_INVOCATIONID_BUILTIN 8
ethannicholas5961bc92016-10-12 06:39:56 -070026
ethannicholasb3058bd2016-07-01 08:22:01 -070027namespace SkSL {
28
29class IRGenerator;
30
31/**
32 * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
Ethan Nicholas941e7e22016-12-12 15:33:30 -050033 * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
ethannicholasb3058bd2016-07-01 08:22:01 -070034 * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
35 * compiled output.
ethannicholas5961bc92016-10-12 06:39:56 -070036 *
37 * See the README for information about SkSL.
ethannicholasb3058bd2016-07-01 08:22:01 -070038 */
39class Compiler : public ErrorReporter {
40public:
41 Compiler();
42
43 ~Compiler();
44
Ethan Nicholas941e7e22016-12-12 15:33:30 -050045 std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text,
46 const Program::Settings& settings);
ethannicholasb3058bd2016-07-01 08:22:01 -070047
Ethan Nicholas941e7e22016-12-12 15:33:30 -050048 bool toSPIRV(const Program& program, SkWStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070049
Ethan Nicholas941e7e22016-12-12 15:33:30 -050050 bool toSPIRV(const Program& program, SkString* out);
51
52 bool toGLSL(const Program& program, SkWStream& out);
53
54 bool toGLSL(const Program& program, SkString* out);
ethannicholasb3058bd2016-07-01 08:22:01 -070055
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050056 void error(Position position, SkString msg) override;
ethannicholasb3058bd2016-07-01 08:22:01 -070057
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050058 SkString errorText();
ethannicholasb3058bd2016-07-01 08:22:01 -070059
60 void writeErrorCount();
61
Ethan Nicholas941e7e22016-12-12 15:33:30 -050062 int errorCount() override {
63 return fErrorCount;
64 }
65
ethannicholasb3058bd2016-07-01 08:22:01 -070066private:
Ethan Nicholas86a43402017-01-19 13:32:00 -050067 void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
68 DefinitionMap* definitions);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050069
Ethan Nicholas86a43402017-01-19 13:32:00 -050070 void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
ethannicholas22f939e2016-10-13 13:25:34 -070071
72 void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
73
Ethan Nicholase1d9cb82017-02-06 18:53:07 +000074 void scanCFG(const FunctionDefinition& f);
ethannicholasb3058bd2016-07-01 08:22:01 -070075
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050076 void internalConvertProgram(SkString text,
ethannicholas5961bc92016-10-12 06:39:56 -070077 Modifiers::Flag* defaultPrecision,
ethannicholasf789b382016-08-03 12:43:36 -070078 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070079
80 std::shared_ptr<SymbolTable> fTypes;
81 IRGenerator* fIRGenerator;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050082 SkString fSkiaVertText; // FIXME store parsed version instead
ethannicholasb3058bd2016-07-01 08:22:01 -070083
ethannicholasd598f792016-07-25 10:08:54 -070084 Context fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -070085 int fErrorCount;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050086 SkString fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -070087};
88
89} // namespace
90
91#endif