blob: 275fc58b3f15cb95d199d51a6f38003b84459ca3 [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
40 std::unique_ptr<Program> convertProgram(Program::Kind kind, std::string text);
41
ethannicholasf789b382016-08-03 12:43:36 -070042 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);
ethannicholasb3058bd2016-07-01 08:22:01 -070049
50 void error(Position position, std::string msg) override;
51
52 std::string errorText();
53
54 void writeErrorCount();
55
56private:
ethannicholas22f939e2016-10-13 13:25:34 -070057 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);
ethannicholasb3058bd2016-07-01 08:22:01 -070066
67 void internalConvertProgram(std::string text,
ethannicholas5961bc92016-10-12 06:39:56 -070068 Modifiers::Flag* defaultPrecision,
ethannicholasf789b382016-08-03 12:43:36 -070069 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070070
71 std::shared_ptr<SymbolTable> fTypes;
72 IRGenerator* fIRGenerator;
73 std::string fSkiaVertText; // FIXME store parsed version instead
74
ethannicholasd598f792016-07-25 10:08:54 -070075 Context fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -070076 int fErrorCount;
77 std::string fErrorText;
78};
79
80} // namespace
81
82#endif