blob: e1bc052fef3eaf0a88666e4a545aeefc15351bdd [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"
ethannicholasf789b382016-08-03 12:43:36 -070018#include "SkSLGLSLCodeGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070019
ethannicholas5961bc92016-10-12 06:39:56 -070020#define SK_FRAGCOLOR_BUILTIN 10001
21
ethannicholasb3058bd2016-07-01 08:22:01 -070022namespace SkSL {
23
24class 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.
ethannicholas5961bc92016-10-12 06:39:56 -070031 *
32 * See the README for information about SkSL.
ethannicholasb3058bd2016-07-01 08:22:01 -070033 */
34class Compiler : public ErrorReporter {
35public:
36 Compiler();
37
38 ~Compiler();
39
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050040 std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text);
ethannicholasb3058bd2016-07-01 08:22:01 -070041
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050042 bool toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070043
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050044 bool toSPIRV(Program::Kind kind, const SkString& text, SkString* out);
ethannicholasf789b382016-08-03 12:43:36 -070045
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050046 bool toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps,
47 SkWStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070048
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050049 bool toGLSL(Program::Kind kind, const SkString& text, const GrGLSLCaps& caps,
50 SkString* out);
ethannicholasb3058bd2016-07-01 08:22:01 -070051
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050052 void error(Position position, SkString msg) override;
ethannicholasb3058bd2016-07-01 08:22:01 -070053
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050054 SkString errorText();
ethannicholasb3058bd2016-07-01 08:22:01 -070055
56 void writeErrorCount();
57
58private:
ethannicholas22f939e2016-10-13 13:25:34 -070059 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);
ethannicholasb3058bd2016-07-01 08:22:01 -070068
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050069 void internalConvertProgram(SkString text,
ethannicholas5961bc92016-10-12 06:39:56 -070070 Modifiers::Flag* defaultPrecision,
ethannicholasf789b382016-08-03 12:43:36 -070071 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070072
73 std::shared_ptr<SymbolTable> fTypes;
74 IRGenerator* fIRGenerator;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050075 SkString fSkiaVertText; // FIXME store parsed version instead
ethannicholasb3058bd2016-07-01 08:22:01 -070076
ethannicholasd598f792016-07-25 10:08:54 -070077 Context fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -070078 int fErrorCount;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050079 SkString fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -070080};
81
82} // namespace
83
84#endif